“无法从UI将值插入到我的数据库表中”

时间:2019-07-07 11:24:12

标签: mysql node.js express

即使查询的响应显示“已插入”,也不会在表登录中插入值,但表不会用新行更新。

var mysql = require('mysql')

router.get('/save',function(req,res){
  var uname = req.query.username;
  var em = req.query.email;
  var pass = req.query.pass;
  var gender = req.query.gender;
  var dob = req.query.date;

  var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"abcd1234",
    database:"nodedb"
  });
  con.query('insert into login(username, email, password, gender, DOB) 
 VALUES("' + uname + '","' + em + '","' + pass + '","' + gender + '","' + dob + '")',function(err,rows,fields){
    console.log(rows);
    res.send("inserted");
  });
  con.end();
});

module.exports = router;

结果显示 “插入” 尽管这些值未插入到我的表格中。

1 个答案:

答案 0 :(得分:-1)

var mysql = require('mysql')

router.get('/save',function(req,res){
  var uname = req.query.username;
  var em = req.query.email;
  var pass = req.query.pass;
  var gender = req.query.gender;
  var dob = req.query.date;

  var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"abcd1234",
    database:"nodedb"
  });

var sql = "INSERT INTO login (username, email, password, gender, DOB) VALUES ('" + uname + "'" + ',' + "'" +  em + "'" + ',' + "'" +  pass + "'" + ',' + "'" +  gender + "'" + ',' + "'" + dob + "')";
    con.query(sql, function (err, result) {
        if (err) throw err;
        console.log("1 record inserted");
        console.log(result);
    });
});

module.exports = router;

尝试上面的代码。... 注意 sql关键字以大写字母表示。 顺便说一句,在nodejs中使用原始sql很累人,因此最好在一段时间内尝试使用ORM。