That is because PHP null
is converted into the empty string "" when you create the query string.
$variable = null;
$insert = "insert into mytable set mycolumn = $variable" ;
echo $insert;
Will produce:
insert into mytable set mycolumn =
To fix your query you would need to check if the PHP variable is null and change it to string NULL. (Also now mentioned in the comment of @MarkB.)
if ($variable == null){
$variable = "NULL";
}
This will produce:
"insert into mytable set mycolumn = NULL"
Note that NULL has no " around it because it is now concatenated to the other string.
*(note: insert into tablename set ..
is not correct, you either insert
data or you update tablename set
data.)