0

search.php 页面只是没有获取结果。只要我按下提交,无论我输入什么字符串,我都会得到一个空白页。无法理解为什么!请帮忙

表单工作正常,但不知何故在提交时给了我一个空白页,而不是结果,我哪里出错了?

form code

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


php code

  <?php

 // Get the search variable from URL
if(isset($_POST['submit']))
{
 $var = $_GET['q'] ;
 $trimmed = trim($var);//trim whitespace from the stored variable

// rows to return


// 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 database
   $con=mysql_connect("localhost","",""); //(host, username, password)

  //specify database 
  mysql_select_db("test",$con) or die("Unable to select database"); //select which database we're using

 // Build SQL Query  
 $query = "select * from questions where question like \"%$trimmed%\"  or detail like \"%   $trimmed%\"     or name like \"%$trimmed%\"
 order by name"; // EDIT HERE and specify your table and field names for the SQL query

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

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
 echo "<h4>Results</h4>";
 echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
 echo "<p><a href=\"http://www.google.com/search?q=" 
 . $trimmed . "\" target=\"_blank\" title=\"Look up 
 " . $trimmed . " on Google\">Click here</a> to try the 
 search on google</p>";
 }

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

   // get results
   $query =  "SELECT * FROM table WHERE question LIKE '%$q%' OR name LIKE '%$q%' or detail like '%$q%'";  
    $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 ;

 // display the results returned
  while ($row= mysql_fetch_array($result)) {
  echo "<table  align=center class=lims>";
  echo "<tr>";
  echo "<th bgcolor=#5D9BCC >QUESTIONS</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['question'] . "</td>";


  echo "<tr>";



  echo "<th bgcolor=#5D9BCC>Alias</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['detail'] . "</td>";

 echo "</tr>";

 echo "<tr>";
 echo "<th bgcolor=#5D9BCC>Code</th>";
 echo "<td bgcolor=#FEE9A9>" . $row['name'] . "</td>";
  echo "</tr>";
 $title = $row["name"];

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

  $currPage = (($s/10) + 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=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

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

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

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

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

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

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

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

3 回答 3

0

POST 键区分大小写。它需要是if(isset($_POST['Submit']))

编辑:没关系上面的答案。问题是表单方法是 GET,而您正在尝试使用 $_POST 访问提交密钥。您需要将其更改为$_GET['Submit'].

于 2012-09-30T22:18:43.600 回答
0

为什么不检查搜索输入的数据是否存在?(由评论请求重新制定:-))

// Get the search variable from URL
if(isset($_GET['q']))

代替

// Get the search variable from URL
if(isset($_POST['submit']))
于 2012-09-30T22:25:14.407 回答
0

您正在检查Submitin $_POST,但表单中指定的方法是GET

要让它工作,请将 PHP 代码更改为查找$_GET['Submit'].

(但我会将所有内容(包括表单方法)移至POST)。

也就是说,您的脚本似乎被粘贴了两次,第二个版本使用$q而不是$trimmed(已$q定义?),以及一个名为“表”而不是“问题”的 SQL 表:

// get results
$query =  "SELECT * FROM table WHERE question LIKE '%$q%' OR name LIKE '%$q%' or detail like '%$q%'";  
 $result = mysql_query($query) or die("Couldn't execute query");
于 2012-09-30T22:26:35.953 回答