[英]Javascript SQL request Bad Field Error in where clause
仅当没有相同的电子邮件大于当前分数的电子邮件没有分数时,该代码才应更新分数,而是显示错误:
Error: Error: ER_BAD_FIELD_ERROR: Unknown column 'li' in 'where clause'
function postScores(useremail, username, scoreValue, leaderboardName) {
if (leaderboardName === "GHOSTS" || leaderboardName === "PACMAN" || leaderboardName === "OVERALL") {
connection.query('UPDATE SCORES_' + leaderboardName + ' SET SCORES=' + scoreValue + ' WHERE SCORES < ' + scoreValue + ' AND USER_EMAIL=' + useremail,
function(err, rows, fields) {
if (err) {
console.log("Failed to Update. Attempting to Insert.");
console.log("Error: " + err);
connection.query(
'INSERT INTO SCORES_' + leaderboardName + '(USER_EMAIL, USER_NAME, SCORES) VALUES (?,?,?)', [
useremail, username, scoreValue
],
function(err, rows, fields) {
if (err) {
console.log("Total Failure. Systems down");
} else {
console.log("Success. Inserted new Scores");
}
});
}
});
} else {
// Reference to Non-Existent Leaderboard
return console.log('Specified Leaderboard of the name ' + leaderboardName + ' does not Exist');
}
}
在UPDATE查询中,不要将原始数据连接到值中,不要使用占位符,并以与INSERT查询相同的方式为值传递数组。
未加引号的字符串值可能会导致SQL语法错误。 通过使用占位符,您不需要处理值中的引号。
connection.query('UPDATE SCORES_' + leaderboardName + ' SET SCORES = ? WHERE SCORES < ? AND USER_EMAIL = ?',
[scoreValue, scoreValue, useremail],
function (err, rows, fields) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.