简体   繁体   English

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

[英]Search filter pagination issue using php and mysql

Here when i am searching key in search filter for shorting row from database based on search filter using pagination its showing correct but when i am clicking for second page its leaving the search filter and catching second query and which is only for pagination, 在这里,当我使用分页在搜索过滤器中搜索基于数据库的基于行的短行时,它显示正确,但是当我单击第二页时,它离开了搜索过滤器并捕获了第二个查询,并且仅用于分页,

Here is my pagination code 这是我的分页代码

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

and here is code when clicking for second page its leaving search session and going to again in normal pagination part 这是代码,当单击第二页时,它的离开搜索会话并在常规分页中再次转到

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

and this is code where fetching the data to table from database 这是从数据库获取数据到表的代码

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

So help me please and I know code is vulnerable to SQL injection that I will prevent this after completing . 因此,请帮助我,我知道代码容易受到SQL注入的攻击,因此我将在完成后防止这种情况的发生。

There are several issues with your code: 您的代码有几个问题:

First of all, when you select the data to display the table, you don't take the search param into consideration, so you will never display records filtered by search. 首先,当您选择显示表的数据时,无需考虑搜索参数,因此您将永远不会显示按搜索过滤的记录。

Also, you don't need to do "SELECT * FROM.." in order to get the count of all records, doing "SELECT COUNT(id) as count FROM.." is much more efficient 另外,您无需执行“ SELECT * FROM ..”即可获取所有记录的计数,而执行“ SELECT COUNT(id)as count FROM ..”则效率更高。

If you are storing the search param in the $_SESSION, make sure you have session_start() on top of your PHP scripts 如果要将搜索参数存储在$ _SESSION中,请确保在PHP脚本之上具有session_start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM