搜索脚本分页无法正常工作

时间:2011-10-26 13:05:31

标签: php mysql html

我有一个搜索脚本,我在网上找到的工作正常,但分页似乎有问题。当我点击'下一步'链接没有任何反应,但我收到此错误'未定义的变量: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>

2 个答案:

答案 0 :(得分: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'] ) : '';
    

答案 1 :(得分:0)

$ _ SERVER ['PHP_SELF']是正确的方法