2017-10-18 53 views
1

我是新手。我無法從前端插入數據到MySQL數據庫。我在插入時遇到問題。我無法找到與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! [email protected] start: `node ./bin/www` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] 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 
+0

什麼_exactly_是'req.body'的內容? 「SHOW CREATE TABLE details」的MySQL輸出是什麼? – hunteke

回答

2

這不是有效的SQL語法。您不能使用一堆這樣的值的佔位符,則必須分別突破出來:

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

一次性將所有這些條件作爲一個佔位符有將它們封裝一個字符串,它打破了內部的影響句法。

重要信息:始終對您的密碼進行哈希處理,切勿使用純文本進行存儲。在絕對至少use Bcrypt這是專門用於密碼,而不是一些像SHA1一樣的高速散列或像MD5一樣的受損散列。

如果您使用的是節點,您可能希望至少考慮使用Passport,它可以爲您做很多繁重的工作,並且可以與其他Node組件搭配使用。

相關問題