脚本宝典收集整理的这篇文章主要介绍了从php插入mysql会将日期字段保存为00:00:00而不是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值为预定值.
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,请注明来意。