我无法用mysql代码找到错误

时间:2017-10-18 04:19:45

标签: mysql node.js

我是新手。我无法将数据从前端插入到mysql数据库中。我在插入时遇到了这个问题。

我的index.js代码

我将表单详细信息存储在变量中并尝试将其插入数据库

router.post('/signup',function(req,res,next) {
    var item = req.body;
    connection.connect(function(err){
        var sql = "INSERT INTO details VALUES ? ";
        connection.query(sql,[item]);
    });
    res.render('data' ,{items:item});
});

错误消息

/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Parser.js:80
        throw err; // Rethrow non-MySQL errors
        ^

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 '`name` = 'Gayathri', `pwd` = 'def'' at line 1
    at Query.Sequence._packetToError (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/gayathri/jsonhbs/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    --------------------
    at Protocol._enqueue (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Connection.query (/home/gayathri/jsonhbs/node_modules/mysql/lib/Connection.js:208:25)
    at Handshake._callback (/home/gayathri/jsonhbs/routes/index.js:46:14)
    at Handshake.Sequence.end (/home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
    at /home/gayathri/jsonhbs/node_modules/mysql/lib/protocol/Protocol.js:225:14
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! jsonhbs@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the jsonhbs@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/gayathri/.npm/_logs/2017-10-18T03_54_05_403Z-debug.log

1 个答案:

答案 0 :(得分:2)

那是无效的SQL语法。您不能使用占位符来表示一堆这样的值,您必须单独将它们分开:

connection.query(
  "INSERT INTO details (name, pwd) VALUES (?,?)",
  [ "Name", "Pwd" ]
);

将所有这些条件作为单个占位符提供,会将它们封装在字符串中,这会破坏语法。

重要:始终哈希密码,绝不使用纯文本来存储密码。至少use Bcrypt是专门用于密码的,而不是像SHA1这样的高速哈希或像MD5这样的受损哈希。

如果你正在使用Node,你可能想要至少考虑使用Passport,它可以为你做很多繁重的工作,并且可以很好地与其他Node组件一起使用。