NodeJS和MySQL,如何在MySQL INSERT语句中使用NodeJS变量

yftpprvb  于 2022-10-03  发布在  Mysql
关注(0)|答案(2)|浏览(234)

所以基本上,我有一个存储这个特定值的JavaScript变量,我需要将这个变量放在MySQL查询语句中。

源代码:

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

我已经尝试了我知道的所有方法,但我仍然无法将变量保存的值插入到数据库中。我该怎么办?

eiee3dmh

eiee3dmh1#

  1. DatabaseConnection.query('INSERT INTO Air_Pollution_Record (Air_Pollution_Reading) VALUES ('+Pollution_Reading+')');
jpfvwuh4

jpfvwuh42#

试着这样做:

  1. //reading post data
  2. const useremail = req.body.useremail;
  3. const password = req.body.password;
  4. //save into db start
  5. var sql = "INSERT INTO `users` (`UserName`, `UserPassword`) VALUES ('"+useremail+"','"+ password+"')";

相关问题