1

the Code is supposed to update the score only if there is no score with the same email greater than the current score, instead it displays the error:

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');
    }
}
2
  • 1
    Should USER_EMAIL have quotes around it (as you're inserting a string)? Commented Oct 10, 2014 at 10:32
  • 1
    Using place holders would be better that quotes. Commented Oct 10, 2014 at 10:34

1 Answer 1

3

In the UPDATE query, don't concatenate raw data into the values, use placeholders and pass an array for the values, in the same way as your INSERT query.

An unquoted string value is likely causing the SQL syntax error. By using placeholders, you don't need to handle quotes in the values.

connection.query('UPDATE SCORES_' + leaderboardName + ' SET SCORES = ? WHERE SCORES < ? AND USER_EMAIL = ?',
    [scoreValue, scoreValue, useremail],
    function (err, rows, fields) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.