0

Getting a syntax error here but can't figure out why?

Have tried using con.escape as well. Gives the same error.

var sql1 = "INSERT INTO Captcha (Captcha_Image) VALUES ('"+imgBase64+"') WHERE Session_ID = '"+x+"'";
  await con.query(sql1, function (err, result) {
    if (err) throw err;
    console.log("1 record inserted");
  });

//Both imgBase64 and x are varchar values and are being stored in correctly

how to solve this error:

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Session_ID = '93e23e3f7d17b1c50107aa6277cb303985e38e1a5faa0a505064806c291a' at line 1

1 Answer 1

2

Insert statements in SQL don't have a WHERE clause, so the following should work without error:

var mysql = require('mysql');
var con = mysql.createConnection({ ... });

var sql1 = "INSERT INTO Captcha (Captcha_Image) VALUES (?)";
con.query({
    sql: sql1,
    timeout: 40000,
},
    [imgBase64],
    function (error, results, fields) {
    }
);

However, a WHERE clause might make sense if you were doing an INSERT INTO ... SELECT, with a select query serving as the source of data to be inserted. Something like this:

INSERT INTO Captcha (Captcha_Image)
SELECT Captcha_Image
FROM some_other_table
WHERE Session_ID = '93e23e3f7d17b1c50107aa6277cb303985e38e1a5faa0a505064806c291a';

If you instead want to change the Captcha image for records which already exist in the table, based on the session id, then you should be doing an update, not an insert:

con.query('UPDATE Captcha SET Captcha_Image = ? WHERE Session_ID = ?',
    [imgBase64, x],
    function (error, results, fields) {
    if (error) throw error;
    console.log('changed ' + results.changedRows + ' rows');
    }
);
Sign up to request clarification or add additional context in comments.

5 Comments

So is there no way to add the value while checking for a particular ID?
What are you trying to check? By definition, the records you are trying to insert aren't even in the database table yet.
They are inserted earlier. This isn't the full code.
If the records are already in the table, and you want to change their values, then you should be doing an update, not an insert. Let me edit my answer again.
I'm sorry for not being clearer. I have a table setup where one column value is filled up while the other fields remain empty. Now I want to select this row using the already entered ID and fill up the rest of the fields.

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.