Reputation: 585
I am trying to find a way to insert a value into one of two columns depending on which one is NULL.
$accept_sth = $dbh->prepare("UPDATE user_properties
IF(followup_offer <> NULL, fstatus=?)
ELSE (istatus=?)
WHERE id=?");
$accept_sth->execute($_POST['option'], $_POST['id']);
I am doing it wrong.
Upvotes: 1
Views: 5029
Reputation: 92785
No ...brain damage... necessary. You can do it with a query
UPDATE user_properties
SET fstatus = IF(followup_offer IS NULL, fstatus, ?),
istatus = IF(followup_offer IS NULL, ?, istatus)
WHERE id = ?
Note: the only possible drawback for some scenarios of this type of query with conditional SET
is that both columns are updated every time (one with a value and one with the old one).
Here is SQLFiddle demo
Upvotes: 3