我有一个(目前是本地主机,但很快将通过 AWS)带有 Express 的 Node.JS 服务器,当我收到以下错误时,我正在尝试通过 MySQL 查询更新 RDS 实例:
{ [Error: ER_PARSE_ERROR: 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 ''history0' = 'http://localhost:3000/' WHERE id = 1' at line 1]
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: '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 \'\'history0\' = \'http://localhost:3000/\' WHERE id = 1\' at line 1',
sqlState: '42000',
index: 0,
sql: 'UPDATE infected SET \'history0\' = \'http://localhost:3000/\' WHERE id = 1;' }
导致错误的 POST 请求:
app.post('/history', function(req, res) {
var hist = 'history' + 0;
var sql = 'UPDATE infected SET ? = ? WHERE id = ?;';
connection.query(sql, [hist, req.body[0].url, 1]);
});
我将hist其用作变量是因为我计划将它放在一个循环中,但我不确定我在这里声明它的方式是否会导致问题,所以我将其保留原样。req.body是JSON.stringify()call on call to的输出chrome.history.search()。所以我试图在索引 0 处获取条目的 URL。
我尝试connection.query使用硬编码字符串直接调用如下:
connection.query("UPDATE infected SET history0='google.com' WHERE id='1'");
并且它成功更新了数据库,所以我认为我如何使用问号将变量插入hist到req.body[0].url查询中存在问题,但我无法弄清楚问题是什么。