this code search all data not from current pagination page

<html>
<body>
<div id="content">
  <style>
.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;
font-weight: bold;
background-color: #000099;
color: #FFF;
}
li {
display:inline-block
}
</style>
  <?php
include"connection.php";
$start=0;
$limit=5;
$id='';
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}

{
if(isset($_POST['Submit'])){
$query=mysql_query("select * from users where FirstName like '%$_REQUEST[fullname]%'");}
else{
$query=mysql_query("select * from users LIMIT $start, $limit");}
echo "<table border='1'>
<tr class='tableheader'>
<th>First Name</th>
<th>Last name</th>
<th>User Id</th>
<th>User Name</th>
<th>Password</th>
<th>Email</th>
<th>Created Date</th>
<th>Gender</th>
</tr>";
while($row=mysql_fetch_array($query))
{
echo "<tr class='tablerow'>";
echo "<td>" . $row['FirstName']      ."</td>";
echo "<td>" . $row['LastName']       ."</td>";
echo "<td>" . $row['UserID']     ."</td>";
echo "<td>" . $row['UserName']       ."</td>";
echo "<td>" . $row['Password']       ."</td>";
echo "<td>" . $row['Email']          ."</td>";
echo "<td>" . $row['CreatedDate']    ."</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "</tr>";    
}
echo "</table></div>";
$rows=mysql_num_rows(mysql_query("select * from users"));
$total=ceil($rows/$limit);
echo "<ul class='page'>";
for($i=1;$i<=$total;$i++)
{
if($i==$id) {
echo "<li class='current'>".$i."</li>"; }
else {
echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
echo "</ul>";

?>
  <form method="post" id="frm" name="frm" action="testa.php">
    <table width="500" border="0">
      <tr>
        <td>Search here:</td>
        <td><input type="text"  name="fullname" id="fullname"/></td>
        </tr>
        <tr>     
        <td><input type="submit" name="Submit" value="Search" /></td>
        <td>&nbsp;</td>
      </tr>

  </form>
  <form style="width:50%"  method="post" id="123" name="1234" >
  <tr>
        <td>Go To Page:</td>
    <td><input type="text" value=""  name="pagination" class=""/></td>
    </tr>
    <tr>
    <td><input name="insert" type="submit" class="button" value="go" /></td>
     </table>
  </form>
  <?php
if(isset($_POST['insert'])){
$Number     =$_POST['pagination'];
$Number=urlencode($Number);
header ("Location:http://localhost:1234/city/testa.php?id=$Number"); 
if ($Number >= $i){
header ("Location:http://localhost:1234/city/testa.php?id=1"); 
exit;
}


}}?>
</div>
</body>
</html>

Dani AI

Generated

Short diagnosis and a concise fix. The search branch in the posted code runs a SELECT without a LIMIT and the pagination count uses the total table row count, so search results are not being paged. The search form uses POST so the search term is not preserved when clicking page links. The header() redirect shown is malformed (it contains HTML) and the code uses deprecated mysql_* calls and unescaped input.

Fix outline (use GET, count filtered rows, then fetch only the page slice, and preserve the search term in links). Example (PDO, prepared statements):

$limit = 5;
$page  = max(1, intval($_GET['page'] ?? 1));
$search = trim($_GET['search'] ?? '');
$where = $search !== '' ? 'WHERE FirstName LIKE :search' : '';

$stmt = $pdo->prepare("SELECT COUNT(*) FROM users $where");
if ($search !== '') { $stmt->bindValue(':search', "%$search%", PDO::PARAM_STR); }
$stmt->execute();
$totalRows = (int)$stmt->fetchColumn();

$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare("SELECT * FROM users $where ORDER BY UserID LIMIT :offset, :limit");
if ($search !== '') { $stmt->bindValue(':search', "%$search%", PDO::PARAM_STR); }
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// build links preserving search:
for ($p = 1; $p <= ceil($totalRows / $limit); $p++) {
    $qs = http_build_query(['page' => $p, 'search' => $search]);
    echo ($p == $page) ? "<span>$p</span>" : "<a href=\"?{$qs}\">$p</a>";
}

Notes and gotchas: use GET for search (or immediately redirect POST to a GET URL so pages can carry the query), validate and clamp the page number, include the same WHERE clause in both the COUNT and SELECT, and always bind parameters to avoid SQL injection. For redirects use plain URLs in header(): for example header('Location: /testa.php?'.http_build_query([...])) then exit; — do not put HTML tags inside header(). This directly addresses the issue raised by and follows 's suggestion to use a clear page query parameter.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

I suggest using the url querystring key of 'page' instead of 'id'. The use of 'id' is confusing.

If your url is something like this...

www.example.com/search.php?page=6

Then your code need only use the $_GET['page'] variable - if it's valid. Otherwise your 'page' value should be set to '1' (default value) or if the $_GET['page'] is an integer but greater than the max number of pages, set to the 'last page' (number of pages). This value could be stored as $page. Example:

<form action="search.php?page=<?php echo $page;?>" method="get">
    <label for="searchbox">Search:</label>
    <input name="search" id="searchbox" />
    <input type="submit" value="search" id="searchbutton" />
</form>

There are a number of ways of doing this.

please send the all code from this i am getting noting

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.