将记录插入到MySQL查询的表语法错误

时间:2019-04-04 20:10:12

标签: mysql sql node.js forms express

我正试图在我的MySQL表中插入一条记录。当我提交表单时,它将发布请求发送到“ / admin”,这会将发布的数据插入到事件表中。

但是,由于Error: ER_PARSE_ERROR,该查询没有插入记录,而是继续给我一个错误。为了解决这个问题,我查看了许多关于mysql的示例插入查询,但我仍然不知道自己在做什么错。有人可以帮我吗?谢谢!

错误图片:

image of error

mySQL表结构的图像: image of mysql table

app.post('/admin', upload.none(), function(req, res) {
        //store the event
    var event_date = convertDateFormat(req.body.date);
    var start_time = convertTimeStringTo24Hours(req.body.starttime);
    var end_time = convertTimeStringTo24Hours(req.body.endtime);
    var sql = 
        "INSERT INTO events"
        + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
        + " VALUES ?";
    var values = [
        req.body.eventname, 
        req.body.description, 
        event_date + " " + start_time,
        event_date + " " + end_time, 
        req.body.location, 
        req.body.attendee, 
        0, 
        ""
    ];
    con.query(sql, values, function(err, result) {
        if (err) throw err
        res.redirect('/admin')
    });
});

1 个答案:

答案 0 :(得分:4)

您需要在表中插入尽可能多的参数,并用括号括起来。

因此您需要替换以下内容:

var sql = 
    "INSERT INTO events"
    + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
    + " VALUES ?";

使用:

var sql = 
    "INSERT INTO events"
    + " (name, description, start_time, end_time, location, max_capacity, hidden_sign_up, other)"
    + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"

就您而言,无需更改其余代码(绑定参数应作为数组传递,而您已经在这样做)。