This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(12个答案)
去年关门了。
我试图更新mysql数据库,但是当我执行查询时,我得到一个错误,其中var id(在where中用作文件名)用作列名。
我读错了吗?我要做什么才能让它工作?
Console Log
var message = '';
var id = req.session.user.id;
console.log(req.session.user.id);
var post  = req.body;
var worker= post.worker;
var farmer= post.farmer;
var soldier= post.soldier;
var defender= post.defender;

var sql= "UPDATE stats SET worker = `"+worker+"`, farmer = `"+farmer+"`, soldier = `"+soldier+"`, defender = `"+defender+"` WHERE `stats`.`id` = `"+id+"` ";

db.query(sql, function(err, results){
  if(results){
    res.send('/home/dashboard.ejs')
  };

  if(err){console.log(err)};
 });
}

最佳答案

试试这个:

var sql= "UPDATE stats SET worker = '"+worker+"', farmer = '"+farmer+"', soldier = '"+soldier+"', defender = '"+defender+"' WHERE id = "+id+" ";

我从末尾的id列名中删除了back ticks(不必要),并用单引号而不是back ticks正确地包围了字符串。也不需要表名。

10-08 15:26