在 MySQL 查询中转义查询值 (node-mysql)

Escaping query values in MySQL Query (node-mysql)

我正在尝试使用 node-mysql Escaping query values 但是当我尝试将它插入我的 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)

回答问题的正确方法是使用 ?可以在 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) {})

参见 referenced answere

中的示例