2017-08-09 48 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_')附近使用正确的语法 –