MySql从节点Js插入错误

时间:2015-07-24 16:12:19

标签: javascript node.js

我有一个像这样的MySQL表:

enter image description here

下面是我试图将数据插入mysql表的代码:

var data = {
    id          : 1,
    custname    : input.custname,
    custemail   : input.custemail,
    ordertype   : input.ordertype,
    noorder     : input.noorder,
    orderdate   : input.orderdate
};

var query = connection.query('INSERT INTO order SET ?', data,      function(err,rows) {
    if (err)
        console.log("Error inserting : %s ",err );

    res.render('createorder', {title :"Order Created Successfully"});
});

但是给了我以下错误:

INSERT INTO order SET `id` = 1, `custname` = 's', `custemail` = 's@gmail.com', `ordertype` = 'Chalbhaja', `noorder` = 2, `orderdate` = '2015-07-24T16:07:58.684Z'
Error inserting : Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order SET `id` = 1, `custname` = 's', `custemail` = 's@gmail.com', `ordertype` =' at line 1               

1 个答案:

答案 0 :(得分:2)

order字是MySQL中的关键字。但是可以有一个名为order的表。您只需在查询中将其转义,例如:

var query = connection.query('INSERT INTO `order` SET ?', data, function(err,rows) {
    if (err)
        console.log("Error inserting : %s ",err );

    res.render('createorder', {title :"Order Created Successfully"});
});