0

Im using mysqljs and I'm trying to UPDATE a table but it shows me an error with my SQL sentence.

the documentation states that

connection.query('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId], function(err, results) {
  // ...
});

But when I try to do it, the query creates all the variables that I'm sending to the first ?, ignorign the other ones, like this

values = [1,2,3,4,5]
sql = 'UPDATE tablename SET col1= ?, col2 = ?, col3= ?, col4 = ? WHERE col5=  ?';

  var query = connection.query(sql, [values], function(err) {
      if (err) {
        console.log(err);
        throw err;
      }
      else {
        connection.end();
      }
  })

But the query that gets executes is:

UPDATE tablename SET col1= 1, 2, 3, 4, col2 = ?, col3= ?, col4 = ? WHERE col5=  ?';

I don't know what I'm doing wrong or what to do to fix it.

1 Answer 1

1

You aren't passing an array of values, you are passing an array of an array of values. Stop doing that.

Sign up to request clarification or add additional context in comments.

4 Comments

to clarify.. connection.query(sql, values, function(err) {
I see, but I was doing that because if I do an INSERT, and pass values like this connection.query(sql, values, function(err) , it shows an error, and dont let me save it
Only inserts the first value, if values=[1,2,3] the query is this INSERT INTO table (col1,col2,col3) VALUES (1), but when I put values between brackets [values] INSERT INTO table (col1,col2,col3) VALUES (1,2,3)
@SebastianChavezIrurzo -- you are saying the behavior of query() is difference based on whether the command is INSERT or UPDATE? I have to say, I am very skeptical. Go back and look at the code again carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.