2011-10-26 9 views
0

オンラインで見つかった検索スクリプトがありますが、ページネーションに問題があるようです。 [次へ]リンクをクリックしても何も起こりませんが、このエラーは「未定義の変数:PHP_SELF」になります。私は間違って何をしていますか? help.Theコードは以下ですしてください:検索スクリプトのページネーションが機能しない

<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

関連する問題