2012-06-07 78 views
0

我有一個MySQL數據庫,並使用多變量搜索頁面find.php輸入變量。結果出來正確(計數是正確的,結果的第1頁也是如此),但是當我嘗試進入下一頁時,我得到一個錯誤::未定義索引:term1行60 ::未定義索引:term2行61等上。作爲設置 Search2.php下面:從多變量查詢mysql數據庫分頁結果

<?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,TERM2等 任何想法的正確信息/幫助理解

+0

www.petefreitag.com/item/451.cfm – Yang

回答

0

你的網頁鏈接不會term1term2等回服務器。另外,如果你打算在鏈接中傳遞它們,那麼你需要檢查$_REQUEST['term1'],$_REQUEST['term2']等,以涵蓋GET和POST請求。

鏈接的代碼應該是這樣的:

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> "; 

如果你有太多的參數傳遞給服務器,那麼你或許應該考慮使用JavaScript的鏈接時發送POST請求。

+0

是有道理的,所以作爲noob我,我會在哪裏插入$ _REQUEST ['term1']等?謝謝 – user1356282

+0

開始使用::: $ term1 = $ _ REQUEST ['term']而不是 – user1356282

+0

謝謝,那麼如何在我的代碼中實現此部分以確保傳遞條款? ::::「你需要檢查$ _REQUEST ['term1'],$ _REQUEST ['term2']等」 – user1356282