0

我有一个 mysql 数据库并使用多变量搜索页面 find.php 来输入变量。结果正确显示(计数正确,结果的第 1 页也是正确的)但是当我尝试转到下一页时出现错误 :: Undefined index: term1 line 60 ::Undefined index: term2 line 61 等等上。Search2.php 设置如下:

<?php

include "db.inc.php";

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

$term1 = $_POST['term1'];
$term2 = $_POST['term2'];
$term3 = $_POST['term3'];
$term4 = $_POST['term4'];

$sql ="SELECT * FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%' LIMIT $start_from, 15";


$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'"); 
$number=mysql_num_rows($query); 
print "<font size=\"5\" color=white><b>CD Requests</b></font> </P>";
?>

分页结构

<?php 

$sql = "SELECT COUNT(id) FROM cdrequests WHERE pname  LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'";  
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0];
$total_pages = ceil($total_records / 15);


/******  build the pagination links ******/
// range of num links to show
$range = 3;



// if not on page 1, don't show back links
if ($page > 1) {
   // show << link to go back to page 1
   echo " <a href='search2.php?page=1'><b>First</b></a> ";
   // get previous page num
   $prev = $page - 1;
   // show < link to go back to 1 page
   echo " <a href='search2.php?page=$prev'><b>&laquo;</b></a> ";
} // end if 



// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $total_pages)) {
      // if we're on current page...
      if ($x == $page) {
         // 'highlight' it but don't make a link
         echo " <font  size='5' color=yellow><b> $x </b></font> ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='search2.php?page=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($page != $total_pages) {
   // get next page
   $next = $page + 1;
    // echo forward link for next page 
   echo " <a href='search2.php?page=$next'><b>&raquo;</b></a> ";
   // echo forward link for lastpage
   echo " <a href='search2.php?page=$total_pages'><b>Last</b></a> ";
} // end if
/****** end build pagination links ******/

echo '</table>';

?>

不知何故,转到第 2 页无法传递变量术语 1、术语 2 等的正确信息。任何想法/帮助表示赞赏

4

1 回答 1

0

Your page links do not pass term1, term2, etc. back to the server. Also if you are going to pass them in the link then you need to check $_REQUEST['term1'], $_REQUEST['term2'], etc. to cover both GET and POST requests.

The code of the links should be like this:

echo " <a href='search2.php?page=$next".
    "&amp;term1=".urlencode($term1).
    "&amp;term2=".urlencode($term2).
    "&amp;term3=".urlencode($term3).
    "&amp;term4=".urlencode($term4)"'><b>&raquo;</b></a> ";

If you have too many parameters to pass to the server then you should probably consider sending POST requests when clicking on the links using JavaScript.

于 2012-06-07T16:39:53.540 回答