NodeJs - mysql插入错误

时间:2015-08-09 10:42:30

标签: mysql node.js

我有以下插入查询:

connection.query('INSERT INTO `items` (`gameID`, `userID`, `bidID`, `value`, `imageUrl`, `itemName`) VALUES (' + gameID + ', 8, ' + rows.insertId + ', 3, https://steamcommunity-a.akamaihd.net/economy/image/class/730/' + ourItems[i].classid + '/150fx125f, ' + ourItems[i].market_name + ')', function(err, rows, fields) {
      if (err) throw err;
});

我收到了以下错误:

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

你应该处理你的字符串和连接:

connection.query("INSERT INTO `items` (`gameID`, `userID`, `bidID`, `value`, `imageUrl`, `itemName`)
    VALUES ('" + gameID + "',
        8,
        '" + rows.insertId + "',
        3,
        'https://steamcommunity-a.akamaihd.net/economy/image/class/730/" + ourItems[i].classid + "/150fx125f',
        '" + ourItems[i].market_name + "'
    )", function(err, rows, fields) {
    if (err) throw err;
});

这在语法上应该是正确的。

答案 1 :(得分:0)

您永远不应该使用这种串联构建查询。 您最有可能最终会遇到一个可能在将来中断的查询,甚至可能会进行SQL注入。

如果您使用mysql模块,那么您应该考虑使用模块(Escaping query values)内置的转义功能:

connection.query('INSERT INTO `items` SET ?', 
      {
         gameID: gameID,
         userID: 8,
         /* ... */
         imageUrl: 'https://steamcommunity-a.akamaihd.net/economy/image/class/730/' + ourItems[i].classid + '/150fx125f'
        /* ... */ 
      }, function(err, rows, fields) {
          if (err) throw err;
      });