我目前正在搜索我的数据库并在每页显示5个结果,然后我打算向用户提供指向其他页面的链接。
当我运行此代码时,它将我的初始查询限制为5条记录,但是当我点击其他页面的链接时,它忽略了我的sql查询参数并重新调整数据库中的所有行。
有谁知道为什么会这样?
这是我的代码:
<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";
?>
答案 0 :(得分:1)
当您点击页码时,您的浏览器正在发出GET
请求,但不会显示school
的值。您可以先将school
添加到请求中,例如
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> ";
}
并确保您可以从school
和GET
获得POST
的值:
$school = $_REQUEST['school']; // instead of $_POST['school']