Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  65] [ 6]  / answers: 1 / hits: 29915  / 6 Years ago, tue, march 20, 2018, 12:00:00

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?


More From » mysql

 Answers
15

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

[#54896] Friday, March 16, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
terrellhunterm questions
Mon, Aug 31, 20, 00:00, 4 Years ago
Mon, Aug 5, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Mon, Mar 11, 19, 00:00, 5 Years ago
;