【问题标题】:How to fix between date to date error in php如何修复php中日期到日期之间的错误
【发布时间】:2021-10-21 19:48:46
【问题描述】:

当我尝试在两个日期之间进行搜索时,reports-details.php 上的所有内容都正常显示,但是当我尝试在 reports-details.php 上重新加载时,显示此错误警告:

在 C:\xampp\htdocs\parking 中为 foreach() 提供的参数无效 第 52 行的预订\管理\reports-details.php

reports.php内容是:

<form action="reports-details.php" method="POST">
     <label>From Date</label>
     <input type="date" name="start_date">
     <label>To Date</label>
     <input type="date" name="end_date">
     <button name="search">Search</button>
</form>


back-end-reports.php

<?php
include 'config.php';
class report extends Connection{
     public function managereport(){
          if (isset($_POST['search'])) {
               $start_date = $_POST['start_date'];
               $end_date = $_POST['end_date'];
               $sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
               $result = $this->conn()->query($sqlselect); 
               $result->execute();
               return $result->fetchAll();
          }
     }
}
$new_vehicle = new report();
$new_vehicle->managereport();
?>



reports-details.php

<?php
     include '../back-end/back-end-reports.php';
     $result = new report(); 
     $query = $result->managereport();
?>

<?php $id = 1; foreach($query as $row) { ?>
 <tbody>
      <tr>
           <td><?php echo $id; ?></td>
           <td><?php echo $row['serial']; ?></td>
           <td><?php echo $row['fullname']; ?></td>
           <td><?php echo $row['num_plate']; ?></td>
           <td><a href="view-ingoing.php?id=<?php echo $row['customers_id']; ?>" class="text-dark">View</a> | <a class="text-dark" href="print.php">Print</a></td>
      </tr>
 </tbody>
 <?php $id++; } ?>

【问题讨论】:

    标签: php mysql date search


    【解决方案1】:

    根据foreach 参考,如果你给任何期望数组 foreach 它将返回该错误,因此这里有两个解决方案

    1.使用前检查foreach参数

    <?php
    
    if(is_array($query){ 
        foreach($query as $row){
            //TODO ...
        }
    }
    
    ?>
    

    2.确保您将数组提供给 foreach

    <?php
    
    if(isset($_POST['search'])) {
        $query = $result->fetchAll();
    }else{
       $query = array();
    }
    
    foreach($query as $row){
        //TODO ...
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:
      <?php $id = 1; foreach($query as $row) { ?>
      

      在使用foreach循环的时候,一定要先判断数组$query,否则会提示报Invalid argument supplied for foreach() 这样的错误,可以在循环前加上判断,

      <?php $id = 1; if (is_array($query) && count($query)) {
       foreach($query as $row) { ?>
       <tbody>
            <tr>
                 <td><?php echo $id; ?></td>
                 <td><?php echo $row['serial']; ?></td>
                 <td><?php echo $row['fullname']; ?></td>
                 <td><?php echo $row['num_plate']; ?></td>
                 <td><a href="view-ingoing.php?id=<?php echo $row['customers_id']; ?>" class="text-dark">View</a> | <a class="text-dark" href="print.php">Print</a></td>
            </tr>
       </tbody>
       <?php $id++; } } ?>
      

      【讨论】:

        【解决方案3】:

        您的问题是函数中的 if 语句

        if (isset($_POST['search'])) {
        

        当您刷新页面时,没有发布数据,因此 managereport 不返回任何内容。 $query 的值因此为空,因此您不能在 foreach 循环中对其进行迭代。

        我的建议是你的 mangerreport() 应该在没有发布数据的情况下返回一个空数组,即

        public function managereport(){
                  if (isset($_POST['search'])) {
                       $start_date = $_POST['start_date'];
                       $end_date = $_POST['end_date'];
                       $sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
                       $result = $this->conn()->query($sqlselect); 
                       $result->execute();
                       return $result->fetchAll();
                  } else {
                       return array();
                  }
             }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多