繁体   English   中英

我正在尝试从此表单中获取值并将其插入使用 mysql 和节点 JS 的表中,但无法获取值

[英]I am trying to take the values from this form and insert it into a table using mysql and node JS, but unable to get the value

我正在尝试获取插入名字和姓氏字段的值,然后将其插入我使用 restAPI 运行的 MySQL 数据库后端。 我错过了什么,所以名字和姓氏实际上被转换为将在 MySql 脚本中使用的 [firstname] 和 [lastname]

表代码是这个

  <div class="superhero">
    <h1>Lets add our first name </h1>
    <form action="/add_user" method="post">
        <input type = "text" firstname = "firstname">
    <h1>Lets add our last name </h1>
    <form action="/add_user" method="post">
        <input type = "text" lastname = "lastname">
        <input type="submit" class="btn btn-primary">
    </form>

然后使用此命令将其带入 nodeJS 服务器

app.post('/add_people', function(req, res){
    axios.get('http://127.0.0.1:5000/api/adduser')
    .then((response)=>{
        var restlist = response.data.results;
        console.log(restlist);
// Now we will execute the results in the page named thanks
    });
});

然后最后它将被带到使用这条路线的 RestAPI

@app.route('/api/adduser', methods = ['POST']) # This is a post method because the user needs to be able to add info
def adding_stuff():
    request_data = request.get_json() # Gets the info from the table and converts to JSON format
    new_fname = request_data['firstname']
    new_lname = request_data['lastname']
    conn = create_connection("", "", "", "")
    sql = "INSERT INTO restaurantusers (firstname, lastname) VALUES ('%s', '%s');" % (new_fname, new_lname) # This sql statement will then be uploaded to the databse to add a new record
    execute_query(conn, sql) # This will execute the query
    return 'Post worked'

抱歉,如果我问的内容听起来很复杂。 教授在 class 中走得太快了,我一直试图找出如何做到这一点,但没有运气。

表格中似乎有两个错误。

  1. 您无需在两个单独的 forms 中拆分名字和姓氏。
  2. 路线add_user与您的发布路线不匹配。

您可能需要进行这些更改

 <div class="superhero">
    <h1>Lets add our first name </h1>
    <form action="/api/adduser" method="post">
        <input type = "text" firstname = "firstname">
    <h1>Lets add our last name </h1>
        <input type = "text" lastname = "lastname">
        <input type="submit" class="btn btn-primary">
    </form>

暂无
暂无

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

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