gpt4 book ai didi

mysql - 我无法找到 mysql 代码的错误

转载 作者:行者123 更新时间:2023-11-29 05:58:51 25 4
gpt4 key购买 nike

我是新手。我无法将数据从前端插入到 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

最佳答案

那不是有效的 SQL 语法。你不能为一堆这样的值使用占位符,你必须单独分解它们:

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

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

重要:始终对您的密码进行哈希处理,切勿使用纯文本来存储它们。绝对至少 use Bcrypt它专门用于密码,而不是像 SHA1 这样的高速散列或像 MD5 这样的受损散列。

如果您正在使用 Node,您可能至少要考虑使用 Passport它可以为您完成很多繁重的工作,并且与其他 Node 组件配合得很好。

关于mysql - 我无法找到 mysql 代码的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46802754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com