I receive via POST the following variables, sometimes it must be a date and sometimes it must be null (for example if that event has not happened yet)
$hora_entrada = $_POST['hora_entrada'];
$salida_comida = $_POST['salida_comida'];
$regreso_comida = $_POST['regreso_comida'];
$hora_salida = $_POST['hora_salida'];
so, I assign NULL into the variables if POST is empty:
if ($hora_entrada == '') {$hora_entrada = "NULL";}
if ($salida_comida == '') {$salida_comida = "NULL";}
if ($regreso_comida == '') {$regreso_comida = "NULL";}
if ($hora_salida == '') {$hora_salida = "NULL";}
now, I want to insert the values into a mysql table:
UPDATE `nomina`.`registro_tiempo` SET
`hora_entrada` = '$hora_entrada',
`salida_comida` = '$salida_comida',
`regreso_comida` = '$regreso_comida',
`hora_salida` = '$hora_salida',
WHERE `registro_tiempo`.`id_tiempo` ='$id_tiempo';
") or die (mysql_error());
the problem is that when the variable is NULL the record says 00:00:00 and I want to keep it NULL
I tried to assign NULL in this two ways without success:
$variable = NULL;
$variable = "NULL";
NOTE: The MySql fields have the NULL value as Predetermined.
¿Do you know any other way to do this? I will apreciate your help
The reason is
'NULL'andNULLare two different things to SQL. You are inserting'NULL'(the literal string NULL) when you should be insertingNULL(The predetermined NULL value)It should be
To do an easy fix is to put logic to wrap the contents with quotes if it isn’t NULL:
And remember to escape your content (especially if it is provided by the user!) See http://php.net/manual/en/security.database.sql-injection.php
EDIT: