Date在php 中提交编辑表单后返回空值
HTML CODE IS
<form class="form-horizontal" action="" method="post" >
<label for="coupon_strt_date" >Start Date</label>
<input type="text" name="coupon_strt_date" id="datepicker" value="<?php echo $cats_rows['coupon_strt_date']; ?>" class='datepick'>
<label for="coupon_exp_date" class="control-label">Exp Date</label>
<input type="text" name="coupon_exp_date" value="<?php echo $cats_rows['coupon_exp_date']; ?>" id="datepicker" class='datepick'>
<input type="submit" name="submit" value="submit">
</form>
<?php
include("config.php");
$tbl_name = "coupons";
if (isset($_POST['submit'])){
$id=$_POST['id'];
$coupon_strt_date = $_POST['coupon_strt_date'];
$coupon_exp_date = $_POST['coupon_exp_date'];
$sql="update $tbl_name set coupon_strt_date = STR_TO_DATE('$_POST[coupon_strt_date]', '%m/%d/%Y'), coupon_exp_date = STR_TO_DATE('$_POST[coupon_exp_date]', '%m/%d/%Y') where id='$id'";
if (!mysql_query($sql))
{
echo "Error Accured";
}
else {
echo " Successfull";
}
}
正在保存数据。。更改日期后,日期值为空。。添加表单运行良好。。但编辑表单是日期值为空。。请给我提供正确的代码。。
试试这个:
<?php
$coupon_strt_date = date('Y-m-d', strtotime($_POST['coupon_strt_date']));
$coupon_exp_date = date('Y-m-d', strtotime($_POST['coupon_exp_date']));
$sql="update $tbl_name set coupon_strt_date = '$coupon_strt_date', coupon_exp_date = '$coupon_exp_date' where id='$id'";
?>
日期需要存储在数据库的YYYY-MM-DD format
中。