Home > Blockchain >  "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v

Time:06-14

I am trying to update MySQL and I am getting an error every time I run it. What am I missing? I have looked at all the posts with similar errors and every change I make I get the same error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE = `lastName` = 'zuniga', `roleID` = 2' at line 1

I have tried:

"UPDATE employees SET ?"

and it just updates everyone on the list. Not just the individual person I ma trying to update.

function updateEmployeeRole() {
  connection.query("SELECT employees.lastName, role.title FROM employees JOIN role ON employees.roleID = role.id;", 
  (err, res) => {
          if (err) throw err;

          inquirer.prompt([
              {
                  name: "lastName",
                  type: "rawlist",
                  choices: function () {
                      var lastName = [];
                      for (var i = 0; i < res.length; i  ) {
                          lastName.push(res[i].lastName);
                      }
                      return lastName;
                  },
                  message: "What is the employee's last name? ",
              },
              {
                  name: "role",
                  type: "rawlist",
                  message: "What is the employee's new title? ",
                  choices: selectRole()
              },
          ]).then(function (answers) {
              var roleId = selectRole().indexOf(answers.role)   1;
              connection.query("UPDATE employees SET WHERE ?",
                  {
                      lastName: answers.lastName,
                      roleID: roleId
                  },
      
                  function (err) {
                      if (err)
                          throw err;
                      console.table(answers);
                      startProcess();
                  });
          });
      });
}

CodePudding user response:

Use this form:

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

https://www.npmjs.com/package/mysql

CodePudding user response:

You need to pass the parameters in an array.

connection.query("UPDATE employees SET ? WHERE ?",
[
 {
   lastName: answers.lastName,
    roleID: roleId
 }, 
 { /* where conditions here */}
],
      
  function (err) {
    if (err)
       throw err;
     console.table(answers);
      startProcess();
   });
  • Related