$ _GET在分页时被销毁

时间:2018-12-01 12:42:32

标签: php

我正在学习PHP。我正在尝试在我的页面之一中使用分页。如果是第一页,则一切正常,但是当我按下一页按钮时,它会给我类似

的错误
Notice: Undefined index: number1 in C:\xampp\htdocs\mycode\compare.php on line 6
Notice: Undefined index: number2 in C:\xampp\htdocs\mycode\compare.php on line 7
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\mycode\compare.php on line 18

我认为这是由于我在第6行和第7行中使用的$ _GET导致的,但是我不知道为什么一旦尝试进入下一页它就会被销毁。我的完整代码如下所示

<?php include("includes/header.php");

    require("includes/function.php");
    require("language/language.php");

      $number1 = $_GET['number1'];
      $number2 = $_GET['number2'];

      $tableName="number_status";   
      $targetpage = "compare.php";   
      $limit = 10;
      $sr=0;

      $query = "SELECT COUNT(*) as num FROM $tableName WHERE number = $number1 OR number = $number2 ";
      $total_pages = mysqli_fetch_array(mysqli_query($mysqli,$query));
      $total_pages = $total_pages['num'];

      $stages = 3;
      $page=0;
      if(isset($_GET['page'])){
      $page = mysqli_real_escape_string($mysqli,$_GET['page']);
      }
      if($page){
        $start = ($page - 1) * $limit; 
      }else{
        $start = 0; 
        } 

     $quotes_qry="SELECT * FROM number_status WHERE number = $number1 OR number = $number2  ORDER BY id DESC LIMIT $start, $limit";

     $result=mysqli_query($mysqli,$quotes_qry);

?>

    <div class="row">
      <div class="col-xs-12">
        <div class="card mrg_bottom">
          <div class="page_title_block">
            <div class="col-md-5 col-xs-12">
              <div class="page_title">Number Details</div>
            </div>

          </div>
          <div class="clearfix"></div>
          <div class="row mrg-top">
            <div class="col-md-12">

              <div class="col-md-12 col-sm-12">
                <?php if(isset($_SESSION['msg'])){?> 
                 <div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <?php echo $client_lang[$_SESSION['msg']] ; ?></a> </div>
                <?php unset($_SESSION['msg']);}?>   
              </div>
            </div>
          </div>
          <div class="col-md-12 mrg-top">
            <table class="table table-striped table-bordered table-hover">
              <thead>
                <tr>                                 
                  <th>#SR</th>
                  <th>Number</th>
                  <th>Online Time</th>
                  <th>Offline Time</th>
                  <th>Duration</th>
                </tr>
              </thead>
              <tbody>
                <?php   
                        $i=0;
                        while($row=mysqli_fetch_array($result))
                        {
                        $number = $row['number'];
                        $start_time = "<span style='color:green;'>".$row['start_time']."</span>";
                        $end_time   = "<span style='color:red'>".$row['end_time']."</span>";                            
                        $tmp = strtotime($row['end_time']) - strtotime($row['start_time']);
                        $duration = floor($tmp/3600) . ":" . floor($tmp/60) . ":" . ($tmp%60);

                ?>
                <tr>

                  <td><?php echo ++$sr+$start;?></td> 
                  <td><?php echo $number ?></td>
                  <td><?php echo nl2br($start_time);?></td>               
                  <td><?php echo nl2br($end_time);?></td>
                  <td><?php echo nl2br ($duration); ?></td>

                </tr>
                <?php

                        $i++;
                        }
                ?> 
              </tbody>
            </table>
          </div>
            <div class="col-md-12 col-xs-12">
            <div class="pagination_item_block">
              <nav>
                <?php if(!isset($_POST["quotes_search"])){ include("pagination.php");}?>                 
              </nav>
            </div>
          </div>
          <div class="clearfix"></div>
        </div>
      </div>
    </div>

<?php include("includes/footer.php");?>  

我的pagination.php就是这样

<?php
                                // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    


    $paginate = '';
    if($lastpage > 1)
    {   
        $paginate .= "<ul class='pagination'>";
        // Previous
        if ($page > 1){
            $paginate.= "<li><a href='$targetpage?page=$prev' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
        }else{
            $paginate.= "<li><span aria-hidden='true'>&laquo;</span></li>"; }



        // Pages    
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<li class='active'><span class='active'>$counter</span></li>";
                }else{
                    $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<li class='active'><span class='active'>$counter</span></li>";
                    }else{
                        $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                }
                $paginate.= "<li>...</li>";
                $paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
                $paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";      
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
                $paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
                $paginate.= "<li>...</li>";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<li class='active'><span class='active'>$counter</span></li>";
                    }else{
                        $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                }
                $paginate.= "<li>...</li>";
                $paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
                $paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";      
            }
            // End only hide early pages
            else
            {
                $paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
                $paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
                $paginate.= "<li>...</li>";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<li class='active'><span class='active'>$counter</span></li>";
                    }else{
                        $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                }
            }
        }

                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<li><a href='$targetpage?page=$next' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>";
        }else{
            $paginate.= "<li><span aria-hidden='true'>&raquo;</span></li>";
            }

        $paginate.= "</ul>";        


}

 // pagination
 echo $paginate;
                                ?>

让我知道是否有人可以帮助我摆脱困境。 谢谢

1 个答案:

答案 0 :(得分:0)

2个“注意”表示已省略查询字符串中的“ page”参数而调用了php页面(因此$ _GET ['page']会导致“ undefined index”消息)。最后一条消息表示“ mysqli_query($ mysqli,$ query)”已返回false,因此您不能将该结果用作“ mysqli_fetch_array”的参数(使用DB函数时应始终检查错误)。由于前一个错误,$ number1和$ number2为空,因此查询出错。