Node JS MySQL - 插入数据..数据库中没有行更新

Node JS MySQL - Inserting Data.. No Rows updates in Database

我正在尝试通过 Node JS 在数据库中插入数据。代码运行良好,显示 "Record Inserted" 消息但没有行在 MySQL.

中更新

这是我执行插入操作的代码

connection.query('SELECT * FROM menu WHERE item_name=\'' + userResponces[2].toLowerCase() + '\'', function(err, rows){
        if (err) throw err;
        else{
            i_id = rows[0].item_id;
            console.log('i_id ' + i_id);


    connection.query('INSERT INTO customer VALUES(default,' + c_name + ',' + c_addr + ',' + c_mob + ')', function(err, res){
        if(err.fatal){
            console.log(''+err.message);
        } 
        else{
            console.log("Record Inserted");


    connection.query('SELECT MAX(customer_id) AS c_id FROM customer', function(err, res){
        if(err) throw err;
        else{
            c_id = parseInt(res[0].c_id) + 1;
            console.log('c_id ' + c_id);


    console.log(i_id + ' ' + c_id + ' ' + qty);
    connection.query('INSERT INTO order1() VALUES(default,' + i_id + ',' + c_id + ',' + qty + ',1)', function(err, res){
        if(err) throw err;
        else
            console.log("Record Inserted");
    });
            }
    });
        }
    });    
            }
    });

在上面的代码SELECT语句中完美运行,所以毫无疑问连接没有错误。这还是为了连接。

var mysql = require('mysql');
 var connection = mysql.createConnection({
   host     : 'localhost',
   user     : 'nodeuser',
   password : 'password',
   database : 'foodorder'
 });

connection.connect(function(err){
if(!err) {
    console.log("Database is connected ...");    
} else {
    console.log("Error connecting database ...");    
}
});

你的第一个条件测试err.fatal。 但是如果查询 returns 出现 SQL 错误,例如 ER_NO_SUCH_TABLE,err 对象没有致命的 属性.

{ [Error: ER_NO_SUCH_TABLE: Table 'bad_table_name' doesn't exist]
  code: 'ER_NO_SUCH_TABLE',
  errno: 1146,
  sqlState: '42S02',
  index: 0 }

所以,在这里,您应该在 err 而不是 err.fatal

上进行测试
connection.query('INSERT INTO customer VALUES(default,' + c_name + ',' + c_addr + ',' + c_mob + ')', function(err, res){
    if (err){
        return console.log(err);
    } 
    else{
        console.log("Record Inserted");
    // ...
});

顺便说一句,想想 escaping values :

connection.query(
   'INSERT INTO customer VALUES(default, ?, ?, ?)',
   [c_name, c_addr, c_mob],
   function(err, res) {
       //...
   }
);