简体   繁体   中英

PHP MySQL Pagination Issue — additional pages display all rows

I am currently searching my db and displaying results 5 per page and then I am intending to provide the user with links to the additional pages.

When i run this code it limits my initial query to the 5 records but then when I click on the links for the additional pages it is ignoring my sql query parameters and retuning all rows in the database.

Does anyone know why this may be happening?

Here is my code:

<html>
<head>
<link rel="stylesheet" type="text/css" href="results.css">
</head>
<img src="http://meritscholarshiplist.com/wordpress/wp- content/uploads/2015/12/searchhead.png" alt="" width="100%" style="padding-top:0px;"/>
<a href="http://meritscholarshiplist.com/wordpress/search-2#searcht|0"><-Search Again</a>
</html>
<?php 
require('./wp-blog-header.php');
$current_user = wp_get_current_user();
$school = $_POST['school'];
$num_rec_per_page=5;
mysql_connect('', '', '');
mysql_select_db('');

if (isset($_GET["page"])) {
    $page  = $_GET["page"];
} else {
    $page=1;
}

;
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%' LIMIT    $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql);
//run the query
while ($array = mysql_fetch_row($rs_result)) {
    echo "<div class='list' style='width:1750px; margin:0 auto;'>";
    echo "<form action='singleview.php' method='post' target='_new$array[0]'><table id='scholarship' style='float:left;' align='center'><thead><th class='head' colspan='3' style='text-align:center;'>$array[2]<br><br>$array[5]<br><br>$array[3]<br><br><input type='hidden' name='id' value='$array[0]'><input type='submit'  value='View More Details'><br>(Will Open in a New Tab)</th></thead>";
    echo "</form></table></div> ";
}

;
?>
<?php
$school = $_POST['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%'";
$rs_result = mysql_query($sql);
//run the query
$total_records = mysql_num_rows($rs_result);
//count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='search.php?page=1'>".'|<'."</a> ";
// Goto 1st page  
for ($i=1; $i<=$total_pages; $i++) {
    echo "<a href='search.php?page=".$i."'>".$i."</a> ";
}

;
echo "<a href='search.php?page=$total_pages'>".'>|'."</a> ";
// Goto last page
echo "<br>Your Search Returned $total_records Results";
?>

When you are clicking on the page number, your browser is making a GET request without the value of school . You can first add school to the request like

echo "<a href='search.php?page=1&school=".urlencode($school)."'>".'|<'."</a> ";
// Goto 1st page  
for ($i=1; $i<=$total_pages; $i++) {
    echo "<a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a> ";
}

and make sure you can get the value of school from both GET and POST :

$school = $_REQUEST['school']; // instead of $_POST['school']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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