2011-10-26 62 views
0

我有一個搜索腳本,我在網上找到這工作得很好,但分頁似乎有問題。當我點擊'下一步'鏈接沒有任何反應,但我得到這個錯誤'未定義變量:PHP_SELF'。我究竟做錯了什麼?請主動幫助代碼如下:搜索腳本分頁不工作

<head> 
    <title>Search pegination</title> 
    <meta name="author" content="Steve R, http://www.designplace.org/"> 
    </head> 
    <body> 

    <form name="form" action="search.php" method="get"> 
     <input type="text" name="q" /> 
     <input type="submit" name="Submit" value="Search" /> 
    </form> 

    <?php 

     // Get the search variable from URL 

     $var = @$_GET['q'] ; 
     $trimmed = trim($var); //trim whitespace from the stored variable 
     $trimmed = mysql_real_escape_string($trimmed); 
    // rows to return 
    $limit=3; 

    // check for an empty string and display a message. 
    if ($trimmed == "") 
     { 
     echo "<p>Please enter a search...</p>"; 
     exit; 
     } 

    // check for a search parameter 
    if (!isset($var)) 
     { 
     echo "<p>We dont seem to have a search parameter!</p>"; 
     exit; 
     } 

    //connect to your database ** EDIT REQUIRED HERE ** 
    mysql_connect("localhost","root",""); //(host, username, password) 

    //specify database ** EDIT REQUIRED HERE ** 
    mysql_select_db("archivesys") or die("Unable to select database"); //select which database we're using 

    // Build SQL Query 
    $query = "select * from archdetaills_tbl where archivedate like \"%$trimmed%\" 
     order by archiveid"; // EDIT HERE and specify your table and field names for the SQL query 

    $numresults=mysql_query($query); 
    $numrows=mysql_num_rows($numresults); 

    // next determine if s has been passed to script, if not use 0 
     if (empty($s)) { 
     $s=0; 
     } 

    // get results 
     $query .= " limit $s,$limit"; 
     $result = mysql_query($query) or die("Couldn't execute query"); 

    // display what the person searched for 
    echo "<p>You searched for: &quot;" . $var . "&quot;</p>"; 

    // begin to show results set 
    echo "Results"; 
    $count = 1 + $s ; 

    // now you can display the results returned 
     while ($row= mysql_fetch_array($result)) { 
     $title = $row["archiveeemail"]; 

     echo "$count.)&nbsp;$title" ; 
     $count++ ; 
     } 

    $currPage = (($s/$limit) + 1); 

    //break before paging 
     echo "<br />"; 

     // next we need to do the links to other results 
    if ($s>=1) { // bypass PREV link if s is 0 
    $prevs=($s-$limit); 
    print "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?s=$prevs&q=$var\">&lt;&lt; 
    Prev 10</a>&nbsp&nbsp;"; 
    } 

// calculate number of pages needing links 
    $pages=intval($numrows/$limit); 

// $pages now contains int of pages needed unless there is a remainder from division 

    if ($numrows%$limit) { 
    // has remainder so add one page 
    $pages++; 
    } 

// check to see if last page 
    if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { 

    // not last page so give NEXT link 
    $news=$s+$limit; 

    echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?s=$news&q=$var\">Next 10 &gt;&gt;</a>"; 
    } 

$a = $s + ($limit) ; 
    if ($a > $numrows) { $a = $numrows ; } 
    $b = $s + 1 ; 
    echo "<p>Showing results $b to $a of $numrows</p>"; 

?> 


<!-- © http://www.designplace.org/ --> 

</body> 
</html> 

回答

0

$ _ SERVER [ 'PHP_SELF']是正確的做法

1

變化$PHP_SELF$_SERVER['PHP_SELF']在:

print "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?s=$prevs&q=$var\">&lt;&lt; Prev 10</a>&nbsp&nbsp;"; 

還有在你的代碼的幾個問題:

  • $trimmed應該逃脫,因爲它是一個潛在的SQL注入:

    $trimmed = mysql_real_escape_string($trimmed); 
    $query = "select * from archdetaills_tbl where archivedate like \"%$trimmed%\" order by archiveid"; 
    
  • 這段代碼可以被縮短:

    $var = @$_GET['q'] ; 
    $trimmed = trim($var); 
    

    分爲:

    $trimmed = isset($_GET['q']) ? trim($_GET['q']) : ''; 
    
更好
+0

選擇是使用庫MySQLi和內置參數綁定安全。 – DampeS8N

+0

非常感謝....你們都非常有幫助。檢出它。 – Nas

+0

是否能解決你的問題,不要忘記投票並標記爲已選擇的答案 – ariefbayu