I am having the issue that I need to be able to put a NULL value in a mysql database. So from the frontend I send a basic javascript object:
{
"Info": {
"Name": "Michelangelo",
"Date" : null
}
}
In my PHP file I do get the request correctly and decode it:
if(isset($postdata) && !empty($postdata)) {
// Extract the data.
$request = json_decode($postdata);
}
It works for strings, booleans, ints correctly but not for null
values. It will output in PHP after decoding like null
and not NULL
. As far as I know the only correct value in PHP is NULL:https://www.php.net/manual/en/language.types.null.php
I put the values like this in DB. Date column is of type date and it accepts NULL
values. However with null
it will be converted to 0000-00-00;
$sql = "UPDATE `users` SET
`Username`='$request->Info->Name',
`Date`='$request->Info->Date' //DB accepts NULL but not null (tested)
WHERE `id` = '{$id}' LIMIT 1";
So why does it not convert correctly? I know I can loop over the object and replace all the null
with NULL
values, but since the object has nesting it will be a major headache. How can I solve this and why does this happen? I would prefer to do this with PHP and not in the sql query.