使用php和mysql搜索过滤器分页问题

时间:2017-11-09 06:46:07

标签: php html mysql

这里当我在搜索过滤器中搜索密钥时,基于搜索过滤器从数据库中缩短行使用分页显示正确但当我点击第二页时它离开搜索过滤器并捕获第二个查询,这仅用于分页,

这是我的分页代码

 <ul class="pagination">
                  <?php
                    for($i=1;$i<=$total_page;$i++)
                      echo"<li class='".($page_id == $i? 'active' : '')."'><a href='index.php?page=".$i."'>$i</a></li>";
                  ?>

              </ul>

这里是点击第二页的代码,它离开搜索会话并再次进入正常的分页部分

<?php
                 $total_num_page=1;
                if(isset($_GET['page']))
                {
                $page_id=$_GET['page'];
                }
                else
                {
                   $page_id=1;
                }
              if(isset($_POST['submitSearch']) || $_SESSION['search'])
                {

                    if($_POST['submitSearch']){
                        $_SESSION['search']=$_POST['search'];  
                    }

                    $all_post_query= "select * from files where recieved_by like '%". $_SESSION["search"]. "%' or processed_by like  '%".$_SESSION["search"]."%' or   purpose like  '%".$_SESSION["search"]."%' or file_name like  '%".$_SESSION["search"]."%' order by date desc";
                    $all_post_run=mysqli_query($con,$all_post_query);
                    $all_post=mysqli_num_rows( $all_post_run);
                    $total_page=ceil($all_post/$total_num_page);
                    $page_start_from=($page_id-1)*$total_num_page;
                 }
                 else
                 {
                   $all_post_query="select * from files order by date desc";
                   $all_post_run=mysqli_query($con,$all_post_query);
                   $all_post=mysqli_num_rows($all_post_run);
                   $total_page=ceil($all_post/$total_num_page);
                   $page_start_from=($page_id-1)*$total_num_page;
                  }?>

这是从数据库中获取数据到表的代码

   <?php
                    $p_query="select * from files order by date desc limit $page_start_from, $total_num_page";
                          $p_run=mysqli_query($con,$p_query);
                            if(mysqli_num_rows($p_run)){
                             while($row=mysqli_fetch_array($p_run))
                             {
                                $c_id=$row['id'];
                                $file=$row['file_name'];
                                $purpose=$row['purpose'];
                                $recieve=$row['recieved_by'];
                                $processed=$row['processed_by'];
                                $address=$row['address'];
                                $contact=$row['contact_no'];
                                $date=$row['date'];


                            ?>
                                            <tr>

                                                <td><a  href="post.php?post_id=<?php echo $c_id?>"><?php echo $c_id;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $file;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $purpose;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $recieve;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $processed;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $address;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $contact;?></a></td>
                                                <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $date;?></a></td>
?>

请帮助我,我知道代码很容易受到SQL注入攻击,我会在完成后阻止它。

1 个答案:

答案 0 :(得分:0)

您的代码存在以下问题:

首先,当您选择要显示表格的数据时,您不会考虑搜索参数,因此您将永远不会显示通过搜索过滤的记录。

此外,您不需要执行“SELECT * FROM ..”以获取所有记录的计数,将“SELECT COUNT(id)作为计数FROM ...”更加高效

如果要将搜索参数存储在$ _SESSION中,请确保在PHP脚本之上有session_start()