I would like to update a row, but I would like to leave unchaged a field when there is not a new value to update the field with.
I know I can write a new query, but my table is quite long and I’m looking for an elegant way to do the job.
Let’s say that part of my long table is the following:
| id | Name | date_added | date_updated |
-----------------------------------------------
| 1 | Review_1 | 2012-02-10 | 2012-02-20 |
I have a form with a checkbox. I would like that if the user checks the checkbox the field date_updated is updated with the current date. But if the checkbox is not checked the field date_updated if left unchanged.
if (isset($_POST['updated'])) {
$updated = [today's date];
}
At the moment my query is this:
mysql_query("UPDATE reviews r, filters f SET
r.title = '$title',
r.description = '$description',
r.date_updated = '$updated',
r.link_homepage = '$homepage'
[etc.]
[etc.]
WHERE r.id = '$id' AND f.id = '$id' ") or die(mysql_error());
You can just leave out the date_updated line if it’s not needed. So:
and then in the query: