Writing MySQL Update Query with NULL value
| From: | Bill Guion | Date: | Mon, 14 Apr 2008 02:24:39 +0000 |
| Subject: | Writing MySQL Update Query with NULL value | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-272961@lists.php.net to get a copy of this message | ||
I'm trying to write a MySQL UPDATE query where one or more variables may be NULL. So, I'm trying something like:
$last_name = $_POST['last_name'];
$first_name = $_POST['first_name'];
$suffix = $_POST['suffix'];
$suffix = empty($suffix) ? NULL : $suffix;
$phone = $_POST['phone'];
$phone_index = $_POST['phone_index'];
$update_query = "UPDATE 'phones'
SET 'last_name' = $last_name, 'first_name' = $first_name,
'suffix' = $suffix, 'phone' = $phone
WHERE 'phone_index' = $phone_index;";
However, when I echo out this query, I get:
UPDATE 'phones' SET 'last_name' = Doe, 'first_name' = John, 'suffix' = , 'phone' = 123-456-7890 WHERE 'phone_index' = 323;
I think, for this to properly update the record, it should be:
UPDATE 'phones' SET 'last_name' = Doe, 'first_name' = John, 'suffix' = NULL, 'phone' = 123-456-7890 WHERE 'phone_index' = 323;
What am I doing wrong?
-----===== Bill =====-----
--
Nothing is so bad that it can't get worse.