简体   繁体   English

如何将数据从Python FLASK输入到数据库?

[英]How do I input data from Python FLASK to a database?

I've got the below Python code: 我有以下Python代码:

@restServer.route("/sendData", methods=['POST'])
def client():
ID = request.form.get('ID')
name = request.form.get('name')
postcode = request.form.get('postcode')
dateofbirth = request.form.get('dateofbirth')
balance = request.form.get('balance')
description = request.form.get('description')

insert_stmt = (
    "INSERT INTO clients (ID, name, postcode, dateofbirth, balance, description) "
    "VALUES (%s, %s, %s, %s, %s, %s)"

)
data = (ID, name, postcode, dateofbirth, balance, description)
cursor.execute(insert_stmt, data)
conn.commit()
print("Did we do it?")
return jsonify(data)

I've also got the below HTML code: 我还获得了以下HTML代码:

  <form method="POST">
         <p>ID of your Avenger:</p> <input type="number" id="ID" required> 
       <p>Name of your Avenger:</p> <input type="text" id="name" required> 
         <p>Post Code of your Avenger:</p> <input type="text" id="postcode" required> 
          <p>Date of Birth of your Avenger:</p> <input type="date" id="dateofbirth" required> 
          <p>Bank balance of your Avenger:</p> <input type="number" id="balance" required> 
          <p>Description of your Avenger:</p> <input type="text" id="description" required> 


        <button class="button1" type="button" id="hashButton" onclick="nameInput()">Submit</button></form>

And finally the below Javascript code: 最后是下面的Javascript代码:

  function nameInput() {
   var words = document.getElementById("ID", "name", "postcode", "dateofbirth", "balance", "description" ).value;
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if(this.readyState == 4 && this.status == 200) {
  var json = JSON.parse(this.responseText);
  document.getElementById("nameInputReturn").innerHTML = 
  json.name;
  }
  }

        xhttp.open("POST", "http://127.0.0.1:5000/sendData", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form- 
 urlencoded");
        xhttp.send(words);
    }

I'm trying to make it so you can input data into the form and then it will upload that data into the database. 我正在尝试制作它,以便您可以将数据输入到表单中,然后将其上传到数据库中。 I've figured out how to input data that I specify in the code, by simply changing the parts variables inside the data = (ID, name, postcode, dateofbirth, balance, description) line. 通过简单地更改data = (ID, name, postcode, dateofbirth, balance, description)行内的零件变量,我已经找到了如何输入在代码中指定的data = (ID, name, postcode, dateofbirth, balance, description) Currently, the submit button is clicked but the database just adds a line which has NULL for every column. 当前,单击了提交按钮,但是数据库仅添加了一行,每列的行均为NULL。

I'm quite new to Flask, so any help is appreciated! 我对Flask还是很陌生,因此可以提供任何帮助!

Your front end is kind of a mess. 您的前端有点混乱。 It seems like you are trying to send the POST request through JS, when your HTML could do it better and easier. 似乎您正在尝试通过JS发送POST请求,而HTML可以更好,更轻松地完成它。

Try removing the listener from your submit button and changing your opening form tag to something like <form method="POST" action="http://127.0.0.1:5000/updateName"> . 尝试从“提交”按钮中删除侦听器,然后将打开的表单标签更改为<form method="POST" action="http://127.0.0.1:5000/updateName">

You should also change all of your id attributes to name . 您还应该将所有id属性更改为name This is because the id attribute does not get sent to the server. 这是因为id属性不会发送到服务器。 The name attribute, however, is sent and used to access the form's information from the server. 但是,name属性已发送并用于从服务器访问表单的信息。

This should be a good place to start: 这应该是一个不错的起点:

<form method="POST" action="http://127.0.0.1:5000/updateName">
  <p>ID of your Avenger:</p> <input type="number" name="ID" required> 
  <p>Name of your Avenger:</p> <input type="text" name="name" required> 
  <p>Post Code of your Avenger:</p> <input type="text" name="postcode" required> 
  <p>Date of Birth of your Avenger:</p> <input type="date" name="dateofbirth" required> 
  <p>Bank balance of your Avenger:</p> <input type="number" name="balance" required> 
  <p>Description of your Avenger:</p> <input type="text" name="description" required> 
  <button class="button1" type="button" id="hashButton">Submit</button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM