用于网站的PHP搜索脚本

时间:2012-12-04 00:27:59

标签: php mysql sql forms

好吧,所以我正在尝试为我的网站编写一个小的PHP搜索脚本,以便用户只需从艺术家姓名,歌曲名称或城市进行搜索。 我的数据库中的表格有“城市”,“艺术家”和“城市”。

这是我的表格:

<div id="search">  
<form name="search" method="post" action="../searchDb.php">  
<input type="text" name="find" placeholder="What are we searching for ?"/> in   
<Select NAME="field">  
<Option VALUE="artist">Artist</option>  
<Option VALUE="song">Song</option>  
<Option VALUE="city">City</option>  
</Select>  
<input type="hidden" name="searching" value="yes" />  
<input type="submit" name="search" value="Search" />  
</form>  
</div>  

如您所见,有三个OPTION值(我的表中每列一个)。 这是我的PHP代码:

<?php  
$searching = "searching";  
$find = "find";  
$field = "field";  
 //this is to make sure the user entered content  
if ($searching =="yes")   
{   
   echo "<p><h2>Results</h2></p>";   

   //if user did not enter anything in the search box, give error   
   if ($find == "")   
   {   
      echo "<p>You forgot to enter a search term</p>";   
   }   

   include 'connect.php';   

   // strip whitespace, non case sensitive  
   $find = strtoupper($find);   
   $find = strip_tags($find);   
   $find = trim ($find);   

   //perform search in specified field  
   $data = mysql_query("SELECT * FROM artists_table WHERE upper($field) LIKE'%$find%'");   

   //show results   
   while($result = mysql_fetch_array( $data ))   
   {   
      echo $result['artist'];     
      echo " ";   
      echo $result['song'];    
      echo "<br>";    
      echo $result['city'];    
      echo "<br>";    
      echo "<br>";   
   }   

   //counts results. ifnone. error    
  $anymatches=mysql_num_rows($data);    
   if ($anymatches == 0)    
   {    
      echo "Sorry, but we can not find an entry to match your query<br><br>";    
   }    

   //show user what he searched.   
   echo "<b>Searched For:</b> " .$find;     
 }     
 ?>     

我的connect.php(包含在内)完美运行(我在同一个文件上运行另一页,没有问题......所以可以说这不是问题)。

当我进行测试并运行搜索时,它会加载我的searchDb.php,但会显示NOTHING。只是一个白页...

非常感谢任何帮助。我迷失了为什么或什么不起作用...... 谢谢伙计们!

1 个答案:

答案 0 :(得分:1)

如果这是您的代码,那么您正在对$searching = "searching"进行硬编码,但在if中,您正在检查是否$searching =="yes",因此不会显示任何代码。

<?php  
$searching = "searching";  
...
...  
//this is to make sure the user entered content  
if ($searching =="yes")   
{   
...
}

编辑 -

我的猜测是你想做类似的事情 -

$searching = mysql_real_escape_string($_POST['searching']); // sanitized just to be consistant.  
$find = mysql_real_escape_string($_POST['find']);  
$field = mysql_real_escape_string($_POST['field']);

注意 - 您不应该使用mysql_*函数编写新代码。您应该了解mysqli_PDO - http://php.net/manual/en/mysqlinfo.api.choosing.php

以下两种方法可以避免获得“通知:未定义的变量”

检查是否按下submit按钮

if (isset($_POST['search'])) {
$searching = mysql_real_escape_string($_POST['searching']); // sanitized just to be consistant.  
$find = mysql_real_escape_string($_POST['find']);  
$field = mysql_real_escape_string($_POST['field']);
}

或者检查每个字段是否已设置,并将其设置为值,如果未设置为no / empty

if (isset($_POST['search'])) {  // checks to see if the form submit button was pushed
$searching = isset($_POST['search']) ? mysql_real_escape_string($_POST['searching']) : 'no'; // sanitized just to be consistant.  
$find = isset($_POST['find']) ? mysql_real_escape_string($_POST['find']) : '';  
$field = isset($_POST['field']) ? mysql_real_escape_string($_POST['field']) : '';
}