I want to update my MYSQL table, but with a condition.
I have a status field and a time field. If status=day, don’t update time field with the current time. if status=night, update time field with current time.
Right now, I am doing a SELECT statement, then an if statement on the returned value in PHP and then performing the UPDATE and changing all fields except the time unless status=night. Is it possible to do this all in one command?
Edit:
Sorry, just to clarify:
A WHERE command will only update the row if status=night. I always want all the fields within the row to be updated, but only update the time field if status=night. If status=day, update the entire row except for the time field.
Code:
if($status=="night") {
$query = "UPDATE post SET name="$name", comment="$comment", status="$status", time="{now()}" WHERE id='$id'";
} else {
$query = "UPDATE post SET name="$name", comment="$comment", status="$status" WHERE id='$id'"
}
1 Answer