2015-08-09 60 views
-1

我有以下INSERT查询:的NodeJS - MySQL的插入错误

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

任何想法?

回答

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; 
}); 

这应该是语法正确。

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; 
     });