将日期插入数据库时如何替换错误的日期?
Posted
技术标签:
【中文标题】将日期插入数据库时如何替换错误的日期?【英文标题】:how to replace the wrong date while inserting date into database? 【发布时间】:2014-12-13 19:14:20 【问题描述】:我正在使用 jquery datepicker,当用户插入一个空的日期值时,它会给我一个默认日期 1970-01-01。我正在尝试更改默认的 jquery datepicker 配置以将默认值设置为 0000-00-00,但我没有找到方法。所以我尝试使用一些选项,将 datepicker 给出的值替换为我的默认值。但它失败了。这是我的代码。
<?php
require_once('auth.php');
?>
<html>
<head>
<title>EXA_mysql</title>
<script type="text/javascript">
function CloseWindow()
window.close();
window.opener.location.reload();
</script>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$con=mysqli_connect("localhost","user","pass","db");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$id_mesin=$_POST['id_mesin'];
$jenama=$_POST['jenama'];
$model=$_POST['model'];
$meter_awal=$_POST['meter_awal'];
$siri_mesin=$_POST['siri_mesin'];
$siri_feeder=$_POST['siri_feeder'];
$siri_finisher=$_POST['siri_finisher'];
$stok_in=$_POST['stok_in'];
$in_do=$_POST['in_do'];
$lokasi=$_POST['lokasi'];
$js_order=$_POST['js_order'];
$stok_out=$_POST['stok_out'];
$out_do=$_POST['out_do'];
$pelangan=$_POST['pelangan'];
$pelangan_alamat=$_POST['pelangan_alamat'];
$pelangan_person=$_POST['pelangan_person'];
$pelangan_no=$_POST['pelangan_no'];
$tech=$_POST['tech'];
$tech_no=$_POST['tech_no'];
$status=$_POST['status'];
$sql="INSERT INTO asset(id_mesin, jenama, model, meter_awal, siri_mesin, siri_feeder, siri_finisher, stok_in, in_do, lokasi, js_order, stok_out, out_do, pelangan, pelangan_alamat, pelangan_person, pelangan_no, tech, tech_no, status) VALUES('$id_mesin', '$jenama', '$model', '$meter_awal', '$siri_mesin', '$siri_feeder', '$siri_finisher', STR_TO_DATE('$stok_in', '%m/%d/%Y'), '$in_do', '$lokasi', '$js_order', STR_TO_DATE('$stok_out', '%m/%d/%Y'), '$out_do', '$pelangan', '$pelangan_alamat', '$pelangan_person', '$pelangan_no', '$tech', '$tech_no', '$status')";
$date=date_create();
if($stok_in == 1970-01-01) "date_date_set($date,0000,00,00)";
if($stok_out == 1970-01-01) "date_date_set($date,0000,00,00)";
$sql2="update asset set(stok_in, stok_out)values('$date_default','$date_default')";
$result=mysqli_query($con, $sql, $sql2);
if($result)
echo "Successful";
echo "<BR>";
echo "<th><form>";
echo "<input type='button' onClick='CloseWindow()' value='Back to Exa_mySQL' align='middle'>";
echo "</form></th>";
else
echo "Data error, please check your submit.";
echo "<BR>";
echo "To view back, press 'Backspace' key. To start new blank form, click on the link below.";
echo "<BR>";
echo "<a href='insert.php'>Back to insert form</a>";
mysqli_close($con);
?>
</body>
</html>
这是我收到的错误。
Warning: mysqli_query() expects parameter 3 to be long, string given in C:\xampp\htdocs\xxx\insert_ac.php on line 60
有什么办法吗?我正在寻求你的帮助。谢谢。
【问题讨论】:
我不确定你是否可以一次调用执行两个sql命令... 【参考方案1】:mysqli_query(http://tr1.php.net/mysqli_query)不能执行两条sql命令,第三个参数是result模式,可以查看mysqli_multi_query(http://tr1.php.net/manual/tr/mysqli.multi-query.php)
编辑:
<?php
require_once('auth.php');
?>
<html>
<head>
<title>EXA_mySQL</title>
<script type="text/javascript">
function CloseWindow()
window.close();
window.opener.location.reload();
</script>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$con=mysqli_connect("localhost","user","pass","db");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$id_mesin=$_POST['id_mesin'];
$jenama=$_POST['jenama'];
$model=$_POST['model'];
$meter_awal=$_POST['meter_awal'];
$siri_mesin=$_POST['siri_mesin'];
$siri_feeder=$_POST['siri_feeder'];
$siri_finisher=$_POST['siri_finisher'];
$stok_in=($_POST['stok_in'] == '1970-01-01') ? "" : $_POST['stok_in'];
$in_do=$_POST['in_do'];
$lokasi=$_POST['lokasi'];
$js_order=$_POST['js_order'];
$stok_out=($_POST['stok_out'] == '1970-01-01') ? "" : $_POST['stok_out'];
$out_do=$_POST['out_do'];
$pelangan=$_POST['pelangan'];
$pelangan_alamat=$_POST['pelangan_alamat'];
$pelangan_person=$_POST['pelangan_person'];
$pelangan_no=$_POST['pelangan_no'];
$tech=$_POST['tech'];
$tech_no=$_POST['tech_no'];
$status=$_POST['status'];
$sql="INSERT INTO asset(id_mesin, jenama, model, meter_awal, siri_mesin, siri_feeder, siri_finisher, stok_in, in_do, lokasi, js_order, stok_out, out_do, pelangan, pelangan_alamat, pelangan_person, pelangan_no, tech, tech_no, status) VALUES('$id_mesin', '$jenama', '$model', '$meter_awal', '$siri_mesin', '$siri_feeder', '$siri_finisher','$stok_in', '$in_do', '$lokasi', '$js_order','$stok_out', '$out_do', '$pelangan', '$pelangan_alamat', '$pelangan_person', '$pelangan_no', '$tech', '$tech_no', '$status')";
$result=mysqli_query($con, $sql);
if($result)
echo "Successful";
echo "<BR>";
echo "<th><form>";
echo "<input type='button' onClick='CloseWindow()' value='Back to Exa_mySQL' align='middle'>";
echo "</form></th>";
else
echo "Data error, please check your submit.";
echo "<BR>";
echo "To view back, press 'Backspace' key. To start new blank form, click on the link below.";
echo "<BR>";
echo "<a href='insert.php'>Back to insert form</a>";
mysqli_close($con);
?>
</body>
</html>
【讨论】:
我已将代码更改为$result=mysqli_query($con, $sql); $result2=mysqli_query($con, $sql2);
,显示成功消息。但我的数据库中仍然有 1970-01-01。
您正在使用 $date_default 变量更新 stok_in 和 stok_out 字段,我们看到的代码部分中没有定义该变量。可以在您设置 $date_default 的位置发布吗?您的表字段 stok_in 和 stok_out 的类型是什么?
我的错。 $date_default
变量应该是 $date
。但是使用$date
变量,我得到这个错误:Catchable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\xxx\insert_ac.php on line 58
.
mysql 中的 stok_in 和 stok_out 字段的类型是什么,如果是日期或日期时间,则不能为它们输入“0000-00-00”值,但可以将它们更新为“允许空值” " 如果用户将这些字段留空,则插入/更新 null。
我的 stok_in 和 stok_out 字段类型是日期。并且已经设置为接收 null 的字段。但是当用户留下空白字段时,日期选择器会将其默认值作为输入。以上是关于将日期插入数据库时如何替换错误的日期?的主要内容,如果未能解决你的问题,请参考以下文章
将日期插入数据库时出现 SQL 语法错误(框架:codeigniter)