I need to update 1 item in 1 row in a database table in mysql using placeholders. The syntax I'm trying to use is:
"UPDATE table SET column_name = newValue WHERE id = selectedID"
I know the table name and I want to select the row to update using the id. I want to pass in the column_name, newValue, and selectedID using placeholders, so I think the statement should look like this:
"UPDATE table SET ?? = ? WHERE id = ?", [{column_name: newValue}, {id: selectedID}]
Here is my code:
employeeToUpdate(columnToUpdate, newValue, employeeToUpdateId) {
console.log("Updating employee information \n");
// console.log('col: ' + columnToUpdate + " newV: " + newValue + " id: " + employeeToUpdateId);
this.connection.query(
// insert a new role with the given information
// "UPDATE employee SET last_name = 'Johnson' WHERE id = 4"
"UPDATE employee SET ?? = ? WHERE id = ?",
[
{
columnToUpdate: newValue
},
{
id: employeeToUpdateId
}
]
)
}
The commented out console.log confirms that the values passed in are correct. The commented out "UPDATE..." statement works for the values provided.
But this results in a parse error. Is there a better way to do this? I'm not finding good documentation to help me.
I think the second parameter should be an array.I could not test it but something like this should work.