表名 - receipt_entry
我的表单中有两个文本框和一个日期选择器
用户输入
receipt = 700
coupon = 501,502,503,504,505,506
coupondate = 28-02-2015
然后在数据库中插入6行,因为文本框中有6个优惠券,并且每个日期还会自动生成6个优惠券,每月+1个月。
插入后运行数据插入如下表
receipt coupon coupondate
700 501 27-03-2015
700 502 27-04-2015
700 503 27-05-2015
700 504 27-06-2015
700 505 27-07-2015
700 506 27-08-2015
我的问题是在插入查询中一切顺利。
但是当我尝试更新相同的内容时,所有列都会正确更新。但优惠券和优惠券没有以增量顺序更新。
我尝试使用收据号更新,因为这在数据库中是唯一的。
UPDATE在下表中运行数据UPDATE
receipt coupon coupondate
700 501 27-03-2015
700 501 27-03-2015
700 501 27-03-2015
700 501 27-03-2015
700 501 27-03-2015
700 501 27-03-2015
以下是我的代码:
编辑链接
<a href="customer_entry.php?edit=<?php echo htmlspecialchars($row['receipt_no']); ?>" class="ico edit">Edit</a>
INSERT CODE
<?php
require_once('includes/config.php');
if(!isset($_SESSION['Auth']['id']))
{
header('Location: index.php');
exit;
}
$errors = array();
if(isset($_POST['save']))
{
$receipt_no = $_POST['receipt_no'];
$coupon = $_POST['coupon'];
$arr = explode(",", $coupon);
$min = min($arr);
$max = max($arr);
$errors = $database->receipt_exists($receipt_no);
if(!count($errors))
{
$startingdate = date("d-m-Y", strtotime($_POST['startingdate']));
for ($i = 1 ; $i <= count($arr) ; $i++)
{
$count = 1;
for ($i = $min; $i <= $max; $i++)
{
$coupondate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($startingdate)) . " +" . $count . " month - 1days"));
$count++;
$insertrow = $database->insertRow("INSERT INTO receipt_entry (coupondate,receipt_no,coupon,startingdate)
VALUES(:coupondate,:receipt_no,:coupon,:startingdate)",array(':coupondate'=>$coupondate,':receipt_no'=>$receipt_no,':coupon'=>$i,':startingdate'=>$startingdate));
}
}
$_SESSION['message'] = "Customer Created Successfully";
}
}
?>
更新代码
<?php
if(isset($_POST['update']))
{
$receipt_no = $_GET['edit'];
$receipt_no = $_POST['receipt_no'];
$coupon = $_POST['coupon'];
$arr = explode(",", $coupon);
$min = min($arr);
$max = max($arr);
$startingdate = date("d-m-Y", strtotime($_POST['startingdate']));
for ($i = 1 ; $i <= count($arr) ; $i++)
{
$count = 1;
for ($i = $min; $i <= $max; $i++)
{
$coupondate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($startingdate)) . " +" . $count . " month - 1days"));
$count++;
$updaterow = $database->updateRow("UPDATE receipt_entry SET coupondate=:coupondate,receipt_no=:receipt_no,coupon=:coupon,startingdate=:startingdate WHERE receipt_no = :receipt_no",
array(':coupondate'=>$coupondate,':receipt_no'=>$receipt_no,':coupon'=>$i,':startingdate'=>$startingdate,':receipt_no'=>$receipt_no));
}
}
}
?>
答案 0 :(得分:0)
检查嵌套循环控制器 ..使用不同的var然后 $ i 进行第二次循环。
for ($i = 1 ; $i <= count($arr) ; $i++)
{
//....
for ($j = $min; $j <= $max; $j++)
{
//.. .....:coupon'=>$j...
}
}