2011-07-13 32 views
0

我曾在很多網站上搜索過,並嘗試過在線提供的不同方式,但無法運行。當我點擊next,last,first,previous時,它不會加載信息。它只加載第一頁的結果。請幫忙!先謝謝你。PHP分頁問題

function retrieveName($fieldName) 
{ 
    $i=1; 
    if(isset($_GET[$fieldName])) 
    { 
     mysql_connect("localhost", "root") or die(mysql_error());   
     mysql_select_db("intern") or die(mysql_error()); 


     //This checks to see if there is a page number. If not, it will set it to page 1 

     if (!(isset($pagenum))) 

     { 

     $pagenum = 1; 

     } 

     //Here we count the number of results 

     $intern = $_GET[$fieldName]; 
     $data = mysql_query("SELECT p.`internName`, p.`internNRIC`, c.`internSchName` FROM `personaldetails` p, `currentinstitution` c WHERE c.`internNRIC`= p.`internNRIC` AND p.`internName` like '%$intern%' || p.`internNRIC` like '%$intern%' || c.`internSchName` like '%$intern%' GROUP BY p.internNRIC") or die(mysql_error()); 

     $rows = mysql_num_rows($data); 

     //This is the number of results displayed per page 

     $page_rows = 1;    


     //This tells us the page number of our last page 

     $last = ceil($rows/$page_rows); 

     //this makes sure the page number isn't below one, or more than our maximum pages 

     if ($pagenum < 1) 

     { 

     $pagenum = 1; 

     } 

     elseif ($pagenum > $last) 

     { 

     $pagenum = $last; 

     } 

     //This sets the range to display in our query 

     $max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

    PRODUCTION. //This is your query again, the same one... the only difference is we add $max into it 

     $data_p = mysql_query("SELECT p.`internName`, p.`internNRIC`, c.`internSchName` FROM `personaldetails` p, `currentinstitution` c WHERE c.`internNRIC`= p.`internNRIC` AND p.`internName` like '%$intern%' || p.`internNRIC` like '%$intern%' || c.`internSchName` like '%$intern%' GROUP BY p.internNRIC $max ") or die(mysql_error()); 


     //This is where you display your query results 

     while($row = mysql_fetch_array($data_p)) 
     {  
      echo $i. "."; 
      echo " NRIC : <a href='InternInfo.php?id='" . $row['internNRIC'] . ">".$row['internNRIC'] ."</a>"; 
      echo "</br><br/>"; 
      echo " Name : ". $row['internName'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name of School :" . $row['internSchName']; 
      echo "</br><br/>";    
      $i++;      
     } 

     echo "<p>"; 


     // This shows the user what page they are on, and the total number of pages 

     echo " --Page $pagenum of $last-- <p>"; 


     // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. 

     if ($pagenum == 1) 

     { 

     } 

     else 

     { 
      echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&searchIntern=$intern'> <<-First</a> ";    
      echo "---Interns Search---";    
      $previous = $pagenum-1;   
      echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&searchIntern=$intern'> <-Previous</a> ";   

     } 

     //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links 

     if ($pagenum == $last) 

     { 

     } 

     else 
     {   
      $next = $pagenum+1;    
      echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&searchIntern=$intern'>Next -></a> ";   
      echo "---Interns Search---";    
      echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&searchIntern=$intern'>Last ->></a> "; 

     } 
    }else echo "Please enter your search."; 
} 
+0

看來你的代碼沒有準備好再次SQL注入。 – Tadeck

回答

1

不是100%這一點,但它看起來像您使用的是$pagenum局部變量,當你想使用一個參數(好主意)或$_GET['pagenum']全局變量這樣的。你也正在開放SQL注入。在所有需要在查詢中使用的變量上使用mysql_real_escape_string(如$ intern)。

+0

我把您的意見和增加$頁次= $ _GET [ '頁次'] \t \t \t如果(!(isset($頁次))) \t \t \t \t \t \t { \t \t \t \t \t \t $頁次= 1; \t \t \t \t \t \t} \t \t \t $頁次= $ _GET [ '頁次']; \t \t \t //在這裏,我們計數結果 數量\t \t \t \t \t \t $實習生= $ _GET [$ fieldName的]; \t \t \t $ data = mysql_query(「SELECT p.'internName',p.'internNRIC',c.'internSchName' FROM'personaldetails' p,'currentinstitution' c WHERE c.'internNRIC' = p.'internNRIC' AND p.'internName' like'%$ intern%'|| p.'internNRIC' like'%$ intern%'|| c.'internSchName' like'%$ intern%'GROUP BY p.internNRIC「)or die (mysql_error()); – simone

+0

但是我得到一個錯誤,說undefined索引:pagenum – simone

+0

你是否在URL中傳遞pagenum? EG:yoursite.thing/index.php?pagenum = 1 – cwallenpoole

0

由於@cwallenpoole說,它看起來像$pagenum範圍外的功能,我猜這個功能是假設register_globals是打開,這通常是a very bad thing。將舊的(繼承的)網站移動到新的服務器時,我發現這導致了很多問題。

要解決的具體問題,請更換:

 if (!(isset($pagenum))) 

    { 

    $pagenum = 1; 

    } 

與此:

$pagenum = isset($_REQUEST['pagenum']) ? 
    (int)$_REQUEST['pagenum'] : 
    1; 

這臺$pagenum請求的pagenum值,默認爲1如果頁碼不在請求。它也會將這個值投射到一個int這應該至少停止一個注入攻擊矢量。其餘的功能是另一回事...