Thursday, April 19, 2018

Using Javascript variables in MYSQL queries



I've been working on this problem for a while and can't seem to find any good info on it.



I'm running a node.js server on an EC2 instance and need to add rows to a MYSQL table with the following code:



client.query('SELECT curattend FROM table1 WHERE ind=1', function(err,result){
att = result[0].curattend;
console.log(att);


client.query("INSERT INTO archive (attendance) VALUES ('att')", function(err,info){
});

console.log(att);

});


I printed 'att' before and after just to be sure...att is equal to '233'. However, the number'0' keeps getting uploaded into the MYSQL table.




Can anyone point me to a resource that can help me solve this?


Answer



Based on user2246674's constructive comments, I also learned something today.



Rather than this:



client.query("INSERT INTO archive (attendance) VALUES (" + att + ")"



Try this instead:



 var att  = result[0].curattend;
client.query("INSERT INTO archive (attendance) VALUES (?);", [att], function(err,info){ });
// This creates the insert statement INSERT INTO archive (attendance) VALUES (att);

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...