Re: Variable value doesn't stay "Null"
| From: | NHans | Date: | Thu, 15 Mar 2001 22:15:58 +0000 |
| Subject: | Re: Variable value doesn't stay "Null" | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-44144@lists.php.net to get a copy of this message | ||
Assuming that column MY_TABLE.MY_NUMERIC_FIELD allows NULLs:
INSERT INTO MY_TABLE (MY_NUMERIC_FIELD, MY_OTHER_FIELD) VALUES ('',
'some_other_value')
will insert a 0 into the column MY_NUMERIC_FIELD in MySQL and many other
databases.
To insert NULL, the SQL must read
INSERT INTO MY_TABLE (MY_NUMERIC_FIELD, MY_OTHER_FIELD) VALUES (NULL,
'some_other_value')
Hence when you build the SQL statement in PHP, check the value that is to be
inserted into MY_NUMERIC_FIELD. If it is NULL as per your requirement, use
the second form of SQL.
You've said is_null($my_form_variable) returned FALSE though as per your
requirement it should have been NULL. Try using empty():
if (empty($my_form_variable) || ! is_numeric($my_form_variable)) {
$value_for_sql = 'NULL';
} else {
$value_for_sql = $my_form_variable;
}
$sql = "INSERT INTO MY_TABLE (MY_NUMERIC_FIELD, MY_OTHER_FIELD) VALUES
($value_for_sql, '$my_other_form_variable')";
mysql_execute($sql);
/* etc */
Hope this helps!