1

我正在尝试使用 node-mysql转义查询值,但是当我尝试将其插入到我的 MySQL 查询中时,我遇到了错误并且似乎找不到问题。我也尝试过以不同的方式布置代码,就像在我链接的 github 示例中看到的那样。

错误:

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 'don\\'t spam!''' at line 1

banReason = 不要垃圾邮件!

var banr = con.escape(banReason)

let sql
sql = `INSERT INTO modlog (senderid, victimid, duration, releasedate, reason) VALUES ('${message.author.id}', '${user}', '${timeCode}', '${moment().add(timeValue, timeType)}', '${banr}'`
con.query(sql)
4

1 回答 1

0

要回答这个问题,正确的方法是使用 ? 可以在node-mysql GitHub 上看到 固定代码:

var values = {senderid: message.author.id, victimid: user, duration: timeCode, releasedate: releaseDate, reason: banReason} 
con.query('INSERT INTO modlog SET ?', values, function(err, result) {})

请参阅参考答案中的示例

于 2020-07-04T00:33:56.213 回答