0

好的,所以我正在尝试为我的网站编写一个小的 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 但没有显示任何内容。只是一个白页...

任何帮助将不胜感激。我不知道为什么或什么不工作......谢谢你们!

4

1 回答 1

1

如果这是您的代码,那么您正在硬编码$searching = "searching",但if您正在检查 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']) : '';
}
于 2012-12-04T00:34:54.453 回答