当我的一列发现用户已经在数据库中时,我想更新它。
这是 viewers = ['freddiejefferson']
表格:
kong_plays 10
freddiejefferson 10
我的代码:
function getPoints(){
for (viewerCount = viewerAmount; viewerCount >= 0; viewerCount--) {
currentViewer = viewers[viewerCount]
connection.query("SELECT * FROM points WHERE twitchName = currentViewer", (err, result) => {
if(err) throw err;
if (result === ''){
connection.query("INSERT INTO points (twitchName, points) VALUES (currentViewer, '10')", (err, result) => {
if(err) throw err;
console.log("Inserted into table")
});
}else{
connection.query("UPDATE points SET points= points+10 WHERE twitchName = currentViewer", (err, result) => {
if(err) throw err;
console.log("updated points")
});
}
});
}
}
它输出的错误: Error: ER_BAD_FIELD_ERROR: Unknown column 'currentViewer' in 'where clause'
错误1:
connection.query("INSERT INTO points (twitchName, points) VALUES (" + currentViewer " +, '10')", (err, result) => {
^^^^^^^^^^^^^ (under current viewer)
SyntaxError: missing ) after argument list
错误2:
throw err; // Rethrow non-MySQL errors
^
Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause'
1条答案
按热度按时间o2rvlv0m1#
看起来你在查询字符串
currentViewer
相反,您希望查询变量currentViewer
持有。这可以通过将查询行更改为:
connection.query("SELECT * FROM points WHERE twitchName = " + currentViewer, (err, result) => {
这同样适用于insert语句:connection.query("INSERT INTO points (twitchName, points) VALUES (" + currentViewer + ", '10')", (err, result) => {