Here is my code
app.put("/update/:id", (req, res) => {
const userID = req.params.id;
const sql = "UPDATE quotes SET `first_name` = ?, `last_name` = ?, `company` = ?, `email` = ?, `phone` = ? WHERE id = ?"
const values = [my values]
db.query(sql, [...values, userID], (err, data) => {
if (err) return res.send(err)
return res.json(data)
})
})
Here is the error I have been receiving
{
"code": "ER_BAD_NULL_ERROR",
"errno": 1048,
"sqlState": "23000",
"sqlMessage": "Column 'first_name' cannot be null",
"sql": "UPDATE quotes SET `first_name` = NULL, `last_name` = NULL, `company` = NULL, `email` = NULL, `phone` = NULL WHERE id = '2'"
}
I want whenever I update first_name
for example it doesn’t affect other fields and keep its record as it is
2
Answers
If you don’t know which values are set in your data you could use this syntax:
COALESCE() returns its first non-NULL argument. So using the format above, if any parameter is NULL, each column will use its own previous value as a default.
try:
each field will either be updated with a new value or keep its existing value if no new value is provided, effectively updating records without deleting fields