2017-08-09 23 views
-1

當我插入具有撇號的HTML輸入我得到這個錯誤ER_PARSE_ERROR

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

例如一個字:插入文字沒有像撇號的「Hello world」的作品成功地和我能看到MySQL表中的行。但是,當我插入「這是」的話,那麼我得到的錯誤。

我使用varchar作爲值行。

connection.query("IN­SERT INTO masstoadmin (`title`,`contact`,`­theuser`,`ptime`,`to­user`,`mass_stat`) VALUES ('"+title+"','"+cont­act+"','"+theuser+"'­,'"+ptime+"','"+tuse­r+"','"+nmasso+"')")­; 
+1

瞭解SQL注入和[如何逃脫值(HTTPS ://github.com/mysqljs/mysql#escaping-query-values)。 – robertklep

+0

@robertklep我試圖使用connection.escapeId,我仍然得到錯誤 –

+0

可能重複[INSERT INTO失敗與節點mysql](https://stackoverflow.com/questions/21779528/insert-into-fails-with- node-mysql) – Veve

回答

2

您應該使用佔位符,這也防止SQL注入通過正確轉義您傳遞到查詢的值:

connection.query("INSERT INTO masstoadmin (`title`,`contact`,`theuser`,`ptime`,`touser`,`mass_stat`) VALUES (?, ?, ?, ?, ?, ?)", [ title, contact, theuser, ptime, tuser, nmasso ], ...) 
+0

我得到這個錯誤connection.query(「INSERT INTO masstoadmin('title','contact','theuser','ptime','touser','mass_stat')VALUES(?,?,? ?,?,?)「,[標題,聯繫人,用戶,ptime,tuser,nmasso]); ^ SyntaxError:無效或意外的標記 –

+0

@darknight請參閱編輯。出於某種原因,您的代碼包含打破代碼的軟連字符。我刪除了它們。 – robertklep

+0

我得到這個錯誤錯誤:ER_PARSE_ERROR:你的SQL語法有錯誤;請檢查與您的MariaDB服務器版本對應的手冊,以便在第1行的'INSERT INTO masstoadmin('title','contact','theuser','ptime','touser','mass_')附近使用正確的語法 –