Q: php paginate not showing on page 2 I searched for similar question and answers on here and only found one but was not confirmed how issue was fixed (will_paginate - other queries not showing on page 2 and beyond)
Basically I had a go at paginate with a test page using a search box and then echo out only one result on each page. Its works on first page but then shows nothing on page 2 on so on. 
Can anyone advise if I am missing something silly or missing the point? On my page its just a text box and search and then I wanted it to paginate if more than one result.
Heres the full code


<?php

 require ("cw/connect.php");
    if(isset($_POST['search'])) {
    $search = mysqli_real_escape_string($connect,$_POST['search']);
    $search = preg_replace("#[^0-9a-z]i#","", $search);
 $start=0;
 $limit=2;
    //Check Form is not blank
 if (empty($search)){
       $msg = "Please enter a search criteria";
 }
 else {
  

    $query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE 
 first LIKE '%".$search."%'
 OR 
 last LIKE '%".$search."%'
 OR 
 email LIKE '%".$search."%'
 OR 
 about LIKE '%".$search."%'
 OR 
 username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");

 $count = mysqli_num_rows($query);
 
 if(isset($_GET['id']))
 {
  $id=$_GET['id'];
  $start=($id-1)*$limit;
 }
 else
 {
  $id=1;
 }
 $page=ceil($count/$limit);
    $query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE 
 first LIKE '%".$search."%'
 OR 
 last LIKE '%".$search."%'
 OR 
 email LIKE '%".$search."%'
 OR 
 about LIKE '%".$search."%'
 OR 
 username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");
  
  
    if($count == 0){
      $msg = "There was no search results!";

    }else{

      while ($row = mysqli_fetch_array($query)) {
    
        $id = $row ['id'];
        $username = $row ['username'];
        $first = $row ['first'];
        $last = $row ['last'];
        $email = $row ['email'];
        $about = $row ['about'];
  
  //The layout and what will be shown on page
        $msg .='<p><b>ID:</b> '.$id.
  '<br><b>User:</b> '.$username.
  '<br><b>First Name:</b> '.$first.
  '<br><b>Last Name:</b> '.$last.
  '<br><b>Email:</b> '.$email.
  '<br><b>About:</b> '.$about.
  '</p>
  <hr>';

      }
   ?>
   
   <ul>
   
   <?php
   if($id > 1) {
   ?>
   
   <li><a href="?id=<?php echo ($id-1);?>">Previous</a></li>
   <?php } ?>
   
   
   <?php
   for($i=1;$i <= $page; $i++)
   {
   ?>
   
  <li><a href="?id=<?php echo $i;?>"><?php echo $i; ?></a></li> 
     
  <?php  
   }
   ?>
   
   <?php
   if($id!=$page)
   {
    
   ?>
   
   <li><a href="?id=<?php echo ($id+1);?>">Next</a></li>
   
   </ul> 
   
 <?php
    }
  }
  }
 }
  ?>
<form action ="" method = "post">
<input name="search" type="text" size="30" placeholder="eg name"/>
<input type="submit" value="search"/> <?php echo "$msg";?>
</form> 

</p>
<br/><br/>



A: I have restructured the code quite a bit. 


*

*I've removed the second query, because the count you do isn't required in that position and can be moved down. Therefore removing a query.

*I've updated your button links with a forward slash, so it doesnt append ? everytime (?id=1?id=1?id=1 after 3 clicks)

*Made the HTML pagination better to read using the built-in php templating system

*The biggest one - you lose your search query after you click on the link, so i have transformed the form to GET and added the parameters to the link of the previous and next pages.


So my result is this - beware - I havent tested it:
require("cw/connect.php");
$msg = "";
if (isset($_GET['search'])) {
    $search = mysqli_real_escape_string($connect,$_GET['search']);
    $search = preg_replace("#[^0-9a-z]i#","", $search);

    if (empty($search)) {
        $msg = "Please enter a search criteria";
    } else {
        $items_per_page = 1;
        $page_id = 1;
        $start = 0;

        // added a little validation
        if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 1) {
            $page_id    = $_GET['id'];
            $start = ceil($page_id * $items_per_page);
        }

        $query = "SELECT id, username, first, last, email, about FROM users WHERE 
            first LIKE '%".$search."%' OR 
            last LIKE '%".$search."%' OR 
            email LIKE '%".$search."%' OR 
            about LIKE '%".$search."%' OR 
            username LIKE '%".$search."%'";

        $result_query = mysqli_query($connect, $query . "LIMIT ".$start.",".$items_per_page) or die ("There are no search results!");
        $count_query = mysqli_query($connect, $query) or die("There are no search results");

        $count = mysqli_num_rows($count_query);
        $page  = ceil($count / $items_per_page);

        if ($count <= 0) {
            $msg = "There where no search results!";
        } else {
            while ($row = mysqli_fetch_array($result_query)) {
                $user_id       = $row ['id'];
                $username = $row ['username'];
                $first    = $row ['first'];
                $last     = $row ['last'];
                $email    = $row ['email'];
                $about    = $row ['about'];

                //The layout and what will be shown on page
                $msg .= '<p><b>ID:</b> '.$user_id.
                    '<br><b>User:</b> '.$username.
                    '<br><b>First Name:</b> '.$first.
                    '<br><b>Last Name:</b> '.$last.
                    '<br><b>Email:</b> '.$email.
                    '<br><b>About:</b> '.$about.
                    '</p><hr>';
            } ?>
            <ul>
                <?php if ($page_id > 1): ?>
                    <li><a href="/?id=<?php echo($page_id - 1); ?>&search=<?php echo $search; ?>">Previous</a></li>
                <?php endif; ?>

                <?php for ($i = 1; $i <= $page; $i++): ?>
                    <li><a href="/?id=<?php echo $i; ?>&search=<?php echo $search; ?>"><?php echo $i; ?></a></li>
                <?php endfor; ?>

                <?php if ($page_id != $page): ?>
                    <li><a href="/?id=<?php echo($page_id + 1); ?>&search=<?php echo $search; ?>">Next</a></li>
                <?php endif; ?>
            </ul>
    <?php }
    }
}
?>
<form action="" method="get">
    <input name="search" type="text" size="30" placeholder="eg name"/>
    <input type="submit" value="search"/>
</form>
<?php echo "$msg"; ?>

