简体   繁体   English

使用 Node 和 mysql。 在字符串变量处插入失败

[英]Using Node and mysql. Insert fails at string variable

I am trying to insert a string variable into my database and the boolean and int go in just fine, but it always tells me我试图在我的数据库中插入一个字符串变量,布尔值和 int 就可以了,但它总是告诉我

error: column "spike" does not exist错误:“尖峰”列不存在

(spike is what is in the string variable) (spike 是字符串变量中的内容)

let sql2 = "INSERT INTO words (gameNumber, wordsOrdered, guessed, unkey) VALUES (2, "+wordsV[1]+", "+guessed[1]+", "+key[1]+" )"
pool.query(sql2, function(err, result){

The wordsV is an array of strings the guessed is a bool and goes in, and the key is an int and goes in. wordsV 是一个字符串数组,猜测是 bool 并输入,键是 int 并输入。

you need escape values你需要转义值

let sql2 = "INSERT INTO words (gameNumber, wordsOrdered, guessed, unkey) VALUES (2, '"+wordsV[1]+"', '"+guessed[1]+"', '"+key[1]+"' )"
pool.query(sql2, function(err, result){

but the best way is to escape the values correctly: https://github.com/mysqljs/mysql#escaping-query-values但最好的方法是正确转义值: https : //github.com/mysqljs/mysql#escaping-query-values

尝试像这样转义您的 sql 值( https://www.databasestar.com/sql-escape-single-quote/ ):

let sql2 = "INSERT INTO words (gameNumber, wordsOrdered, guessed, unkey) VALUES (2, '"+wordsV[1]+"', '"+guessed[1]+"', '"+key[1]+"' )"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM