Having trouble updating a value in my Database, as it's resulting in an error.
Backend:
router.patch("/toggleState", (req, res) => {
const todoId = req.body.todoId;
const attribute = req.body.attribute;
const newValue = req.body.newValue;
const sql = "UPDATE posts SET ? = ? WHERE todo_id = ?";
db.query(sql, [attribute, newValue, todoId], (err, result) => {
if (err) throw err;
res.send(result);
});
});
Frontend:
const toggleState = (todo_id, attribute, newValue) => {
if (userId) {
console.log("ATTRIBUTE: ", attribute);
//Axios.patch(`${apiUrl}/todo/toggleState`, { todoId: todo_id, attribute: attribute, newValue: newValue });
}
dispatch({ type: actionTypes.toggleState, payload: { todo_id, attribute } });
};
Error: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''important' = 1 WHERE todo_id = 1' at line 1"
After SQL processing: sql: "UPDATE posts SET 'important' = 1 WHERE todo_id = 1"
Noticing that there are ' ' apostrophes surrounding my attribute variable (important), which seems to be causing the error. Although, I've checked the variable both in the frontend and backend console logs, and it's of type string without any apostrophes. Any ideas?
Also, when attempting the same SQL string in mysql-workbench with real values instead of variables, it works fine.
Running react/axios on the frontend and Node/Express with MySQL on the backend.