title: Nodejs: get max id from table directly on insert
body: <p>I'm trying to insert a new value in a table (using node.js) and at the same time get the ID to be inserted, using a select MAX from table. First I tried this:</p>
  
  <pre><code>var nova = {
      Id: 'Select MAX(Id)+1 from TABELA',
      Id_novo: dados_da_msg[1],
      Nome: dados_da_msg[2],
  };
  connection.query('INSERT INTO TABELA SET ?', nova, function(err, result) {
      if (err)
          console.log("ERROR23: " + err.message);
  });
  </code></pre>
  
  <p>which didn't worked. It always returned 0 and violated the primary key.
  Then I tried this</p>
  
  <pre><code>var sql = "INSERT INTO TABELA (Id, Id_novo, Nome) Select MAX(Id)+1,'"+dados_da_msg[1]+"','"+dados_da_msg[2]+"' from TABELA;";
  connection.query(sql, function(err, result) {
      if (err)
          console.log("ERROR23: " + err.message);
  });
  </code></pre>
  
  <p>which worked, but is vulnerable to SQLinjections attacks. Anybody knows how can I achieve this using the first syntax?</p>