Php 搜索脚本不工作

Php 搜索脚本不工作,php,mysql,database,Php,Mysql,Database,php页面无法获取结果。只要我按下submit键,不管我输入了什么字符串,我都会得到一个空白页。 我不明白为什么! 请帮忙 表单运行良好,但不知怎么的,在提交时它给了我一个空白页,而不是结果,我哪里出错了 form code <form name="form" action="../search.php" method="get"> <input name="q" type="text" value="type!" onfocus="this.value='

php页面无法获取结果。只要我按下submit键,不管我输入了什么字符串,我都会得到一个空白页。 我不明白为什么! 请帮忙

表单运行良好,但不知怎么的,在提交时它给了我一个空白页,而不是结果,我哪里出错了

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);
}
?>
表单代码
PHP代码

POST键区分大小写。如果(isset($\u POST['Submit']),则需要设置为
if


编辑:不要在上面回答。问题是表单方法是GET,您试图使用$\u POST访问提交密钥。您需要将其更改为
$\u GET['Submit']

为什么不检查来自搜索输入的数据是否存在?(通过征求意见重新制定:-)

而不是

// Get the search variable from URL
if(isset($_POST['submit']))

您正在
$\u POST
中检查
Submit
,但表单中指定的方法是
GET

要使其工作,请将PHP代码更改为查找
$\u GET['Submit']

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

这就是说,您的脚本似乎已经粘贴了两次,第二个版本使用了
$q
而不是
$trimmed
(是否定义了
$q
),以及一个名为“table”而不是“questions”的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");

数据库字段是“id”“name”“question”“detail”,其中包含各种注入。而且,html的现代化也不会带来什么好处hurt@damien:什么是html的现代化,你能解释一下吗?这不是“验证”,不过,只是检查一下。当然,我不知道他的业务规则是什么!:)他可能需要额外的验证!但至少他会知道变量存在编辑:哦,天哪,老实说,我没有读过第一个
if
。。。请上帝帮助他…非常感谢。在用GET替换OST后,它工作了,是的,q没有定义,所以将其更改为trimmed。我想知道更多的事情,如果我打扰你,很抱歉。但是如果我想在搜索表单的同一页上显示结果呢?这要复杂得多。您需要使用AJAX;表单按钮提交一个AJAX查询,但页面不会重新加载;相反,HTML中的某些DIV将其内容替换为对搜索PHP脚本的“隐藏”调用的结果。不过,您需要获得一个好的AJAX教程。另一种方法,尽管可以说没有那么简洁,但将在显示结果的同一个PHP文件中生成表单(与示例中的代码类似,假设它都在一个文件中)。然后用户将看到表单,然后是结果。
// 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");