Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ER_PARSE_ERROR: You have an error in your SQL syntax;

I'm trying to insert values using mysql in nodejs. I had written the following code and installed MySQL support via npm,But canot to INSERT INTO the table due to this problem.

My code;

var mysql = require('mysql');

var values=randomValueHex(8);

var sql = "INSERT INTO `activationkeys`(`activationKey`, `productId`) 
VALUES ( values ,'3')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});

My Error on terminal:

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 ''3')'

How can i solve this problem?

like image 729
Manoj A Avatar asked Oct 21 '25 21:10

Manoj A


2 Answers

Why are you using back quote for the column names? We do not need that in column names. You can simply create your dynamic sql query by using + operator on the column values like this:

var sql = "INSERT INTO activationkeys (activationKey, productId) VALUES ( " + values + " ,'3')";
like image 175
Ankit Agarwal Avatar answered Oct 24 '25 10:10

Ankit Agarwal


Instead of

var sql = "INSERT INTO `activationkeys`(`activationKey`, `productId`) 
VALUES ( values ,'3')";

Please try this

 var sql = "INSERT INTO `activationkeys`(`activationKey`, `productId`) 
    VALUES ( " +  values + " ,'3')";

provided values is a string

like image 28
UchihaItachi-Inactive-Account Avatar answered Oct 24 '25 11:10

UchihaItachi-Inactive-Account



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!