Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs and MySQL, How to use NodeJs variable in MySQL INSERT statement

Tags:

node.js

mysql

So basically, I have this JavaScript variable which stores this certain value, and I need to put this variable in MySQL Query statement.

Source Code:

var Pollution_Reading = 25;
DatabaseConnection.query('INSERT INTO Air_Pollution_Record (Air_Pollution_Reading) VALUES ('"Pollution_Reading"')');

I've tried every way that I know, but I still can't insert the value that the variable is holding into the database. What should I do?

like image 700
Marc Avatar asked Mar 12 '26 17:03

Marc


2 Answers

DatabaseConnection.query('INSERT INTO Air_Pollution_Record (Air_Pollution_Reading) VALUES ('+Pollution_Reading+')');
like image 107
Sagar Jajoriya Avatar answered Mar 14 '26 07:03

Sagar Jajoriya


Try doing this:

//reading  post data
const useremail = req.body.useremail;
const password = req.body.password;
//save into db start
var sql = "INSERT INTO `users` (`UserName`, `UserPassword`) VALUES ('"+useremail+"','"+ password+"')";
like image 21
Jay Bharat Avatar answered Mar 14 '26 07:03

Jay Bharat