使用for循环的递增日期的php代码在更新条件下不起作用

标签 php mysql

表名 - receipt_entry

我的表单中有两个文本框和 1 个日期选择器

用户输入

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

我的问题是插入查询一切顺利。

但是当我尝试更新相同的内容时,所有列都已正确更新。但是 coupon 和 coupondate 没有按 incrementorder 更新。

我尝试使用收据号进行更新,因为这在数据库中是唯一的。

在 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>

插入代码

<?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));
                }
            }   

        }                   
?>

最佳答案

检查您的嵌套循环 Controller .. 在第二个循环中使用不同的变量然后使用 $i

for ($i = 1 ; $i <= count($arr) ; $i++) 
{
  //....
  for ($j = $min; $j <= $max; $j++)
  {
     //.. .....:coupon'=>$j...
  }
}

关于使用for循环的递增日期的php代码在更新条件下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29125010/

相关文章:

php - mysql中如何连接两个表的记录

php - 使用 PHP 查找所有 XML 命名空间 URI

php - PHP Mysql-使用分组依据的交易

php - SQL获取同组其他用户数据

php - 如何在 Controller 扩展中转换 SilverStripe 变量

php - MySQL PHP PDO 准备语句 - 性能问题与安全性

mysql - 我可以使用 MySQL Workbench 来创建 MariaDB 吗?

php - 通过单击在 xampp 中运行 php 项目

mysql - 在 mysql 上对数百万行进行 SUM() 和 GROUP BY

php 和 sql 命令使我的服务器崩溃