在php中创建搜索框

时间:2013-03-27 10:13:46

标签: php html mysql xampp

这是我在我的数据库中对搜索框的编码,但是当我运行它时会显示错误提示:未定义的变量:在第15行的/opt/lampp/htdocs/1234.php中搜索 然后我在搜索框中输入任何内容 它说 找不到对象!

在此服务器上找不到请求的URL。引用页面上的链接似乎是错误的或过时的。请告知该页面的作者有关错误的信息。

如果您认为这是服务器错误,请与网站管理员联系。

错误404

本地主机 Apache / 2.4.3(Unix)OpenSSL / 1.0.1c PHP / 5.4.7

  <html>
  <h2>Search</h2> 
  <form name="search" method="post" action="<?=$PHP_SELF?>">
  Seach for: <input type="text" name="find" /> in 
  <Select NAME="field">
  <Option VALUE="fname">diseasename</option>
  <Option VALUE="lname">genename</option>
  </Select>
  <input type="hidden" name="searching" value="yes" />
  <input type="submit" name="search" value="Search" />
  </form>
  </html>
  <?php 
  //This is only displayed if they have submitted the form 
  if ($searching =="yes") 
  { 
  echo "<h2>Results</h2><p>"; 

  //If they did not enter a search term we give them an error 
  if ($find == "") 
  { 
  echo "<p>You forgot to enter a search term"; 
  exit; 
  } 

  // Otherwise we connect to our Database 
  mysql_connect("localhost", "root", "****") or die(mysql_error()); 
  mysql_select_db("missensencemuttation") or die(mysql_error()); 

  // We preform a bit of filtering 
  $find = strtoupper($find); 
  $find = strip_tags($find); 
  $find = trim ($find); 

  //Now we search for our search term, in the field the user specified 
  $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); 

  //And we display the results 
  while($result = mysql_fetch_array( $data )) 
  { 
  echo $result['fname']; 
  echo " "; 
  echo $result['lname']; 
  echo "<br>"; 
  echo $result['info']; 
  echo "<br>"; 
  echo "<br>"; 
  } 

  //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
  $anymatches=mysql_num_rows($data); 
  if ($anymatches == 0) 
  { 
  echo "Sorry, but we can not find an entry to match your query<br><br>"; 
  } 

  //And we remind them what they searched for 
  echo "<b>Searched For:</b> " .$find; 
  } 
  ?> 

我不知道我在剧本中做错了什么。我是php的初学者,我正在使用互联网参考获取php.can中的知识一个正确的脚本

2 个答案:

答案 0 :(得分:0)

使用如下:

extract($_POST);
 if ($searching =="yes") 

答案 1 :(得分:0)

目前,脚本中未定义

$searching。我想你的意思是$_POST['searching']

在比较周围添加if (isset($_POST['searching'])) { //old if }以确保$_POST['searching']已设置,并将$searching替换为$_POST['searching']

编辑:将$PHP_SELF替换为$_SERVER['PHP_SELF'],这可以帮助您解决问题。