从php插入mysql会将日期字段保存为00:00:00而不是NULL

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了从php插入mysql会将日期字段保存为00:00:00而不是NULL脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过POST收到以下变量,有时它必须是一个日期,有时它必须为null(例如,如果该事件尚未发生)

$hora_enTrada = $_POST['hora_enTrada'];
$salida_comida = $_POST['salida_comida'];
$regreso_comida = $_POST['regreso_comida'];
$hora_salida = $_POST['hora_salida'];

所以,如果POST为空,我将NULL分配给变量:

if ($hora_enTrada == '') {$hora_enTrada = "NULL";}
if ($salida_comida == '') {$salida_comida = "NULL";}
if ($regreso_comida == '') {$regreso_comida = "NULL";}
if ($hora_salida == '') {$hora_salida = "NULL";}

现在,我想将值插入到MysqL表中:

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());

问题是,当变量为NULL时,记录显示为00:00:00,我想将其保持为NULL

我尝试以这两种方式分配NULL但没有成功:

$VARiable = NULL;

$variable = "NULL";

注意:MysqL字段的NULL值为预定值.

¿你知道其他任何方法吗?我会帮助你

解决方法

原因是’NULL’和NULL是sql的两个不同的东西.你应该插入NULL(预定的NULL值)时插入’NULL'(文字字符串NULL)

UPDATE  `nomina`.`registro_tiempo` SET  
`hora_enTrada` =  'NULL',`salida_comida` =  'NULL',`regreso_comida` =  'NULL',`hora_salida` =  'NULL',WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
") or die (MysqL_error());

它应该是

UPDATE  `nomina`.`registro_tiempo` SET  
`hora_enTrada` =  NULL,`salida_comida` =  NULL,`regreso_comida` =  NULL,`hora_salida` =  NULL,WHERE  `registro_tiempo`.`id_tiempo` ='$id_tiempo';
") or die (MysqL_error());

要做一个简单的修复就是把逻辑用引号括起来,如果它不是NULL:

<?PHP
$var = $var == "" ? "'" . $var . "'" : "NULL";
$sql = "INSERT INTO MyTable VALUES ('$id',$var)";
?>

并记住要逃避您的内容(特别是如果它是由用户提供的!)见http://php.net/manual/en/security.database.sql-injection.php

编辑:

<?PHP
// If the variable is not null/empty,wrap quotes around IT. Otherwise,Store as NULL
$hora_enTrada = $_POST['hora_enTrada'] == "" ? "'" . $_POST['hora_enTrada'] . "'" : "NULL";
$salida_comida = $_POST['salida_comida'] == "" ? "'" . $_POST['salida_comida'] . "'" : "NULL";
$regreso_comida = $_POST['regreso_comida'] == "" ? "'" . $_POST['regreso_comida'] . "'" : "NULL";
$hora_salida = $_POST['hora_salida'] == "" ? "'" . $_POST['hora_salida'] . "'" : "NULL";

// You should also MysqLi_real_escaPE_string them
$hora_enTrada = MysqLi_real_escape_string($hora_enTrada);
$salida_comida = MysqLi_real_escape_string($salida_comida);
$regreso_comida = MysqLi_real_escape_string($regreso_comida);
$hora_salida = MysqLi_real_escape_string($hora_salida);

"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());
?>

脚本宝典总结

以上是脚本宝典为你收集整理的从php插入mysql会将日期字段保存为00:00:00而不是NULL全部内容,希望文章能够帮你解决从php插入mysql会将日期字段保存为00:00:00而不是NULL所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。