Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环的增量日期的php代码在更新条件下不工作_Php_Mysql - Fatal编程技术网

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

使用for循环的增量日期的php代码在更新条件下不工作,php,mysql,Php,Mysql,表名称-收货单条目 我的表单中有两个文本框和一个日期选择器 用户输入 receipt = 700 coupon = 501,502,503,504,505,506 coupondate = 28-02-2015 然后在数据库中插入6行,因为文本框中有6张优惠券,并且每个日期自动生成6张coupondate,每个日期+1个月 在insert之后,在如下表中运行数据insert receipt coupon coupondate 700 50

表名称-收货单条目

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

用户输入

receipt      =  700
coupon       =  501,502,503,504,505,506
coupondate   =  28-02-2015
然后在数据库中插入6行,因为文本框中有6张优惠券,并且每个日期自动生成6张coupondate,每个日期+1个月

在insert之后,在如下表中运行数据insert

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
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
我的问题是插入查询中的每件事情都进行得很顺利

但当我尝试更新相同的内容时,所有列都会正确更新。但优惠券和coupondate并没有以增量方式更新

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

更新后,在下表中运行数据更新

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

        }                   
?>

检查嵌套循环控制器。。然后对第二个循环使用不同的var$i

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

for($i=1;$i
$coupondate=date(“d-m-Y”,strotime(date(“d-m-Y”,strotime($startingdate)))。“+”$count.“month-1days”);
你的很多日期代码基本上都是醉醺醺的。你在时间戳和字符串之间摇摆不定。你的“源代码”值应该始终是时间戳。只有在显示时间时才转换为字符串。并且将这些日期作为字符串存储在数据库中。这几乎总是一个坏主意。现在不能对它们使用mysql日期函数,因为它们不是日期/日期时间字段。