1

I want to execute multiple insert queries with LAST_INSERT_ID() My table1 have AUTO_INCREMENT on id_column:

var valueTable1 = {value1: 'foo'};
var valueTable2 = {value2: 'bar'};

connection.query('INSERT INTO table1 SET ?; ' +
                 'INSERT INTO table2 SET ?, `id_table_1`=LAST_INSERT_ID();',
                 [valueTable1, valueTable2]});

I have error : ER_PARSE_ERROR: You have an error in your SQL syntax;

What might be the problem here?

EDIT: I finally succeeded:

var valueTable1 = {value1: 'foo'};
var valueTable2 = {value2: 'bar'};

connection.query('INSERT INTO table1 SET ?', valueTable1, function(err,sqlInfo){
    if(err){
        console.error(err);
    }else{
        valueTable2.id_value = sqlInfo.insertId;
        connection.query('INSERT INTO table2 SET ?', valueTable2,function(err){
            if(err){
                console.error(err);
            }
        } 
    }
});
Rayms
  • 23
  • 5
  • what are the values of valueTable1 and valueTable2 ? – Jérôme Jun 23 '16 at 13:04
  • The problem is completely invalid SQL syntax. You can't pass column names and their values in single parameter, hoping it will work out of the blue. Do you know SQL at all? – Mjh Jun 24 '16 at 13:56
  • You can, i saw that here :https://github.com/mysqljs/mysql#escaping-query-values – Rayms Jun 27 '16 at 12:40

0 Answers0