-1

我得到了这个用于搜索的代码,但我遇到了问题。它运行良好,但在我单击搜索选项卡后,它显示错误。但是,如果我尝试输入查询进行搜索,它工作正常。

这些是错误:

Notice: Undefined index: searching in F:\Programs\wamp\www\a\search.php on line 45
Notice: Undefined index: find in F:\Programs\wamp\www\a\search.php on line 46
Notice: Undefined index: field in F:\Programs\wamp\www\a\search.php on line 47

这是代码:

<h2>Search</h2> 
 <form name="search" method="post" action="search.php">
 Seach for: <input type="text" name="find" /> in 
 <Select NAME="field">
 <Option VALUE="firstname">First Name</option>
 <Option VALUE="lastname">Last Name</option>
 <Option VALUE="location">Location</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>


 <?php 


 $searching = $_POST['searching'];
 $find = $_POST['find'];
 $field = $_POST['field'];

 //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", "root") or die(mysql_error()); 
 mysql_select_db("chess") 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 members WHERE upper($field) LIKE'%$find%'"); 

 echo "<table border=1>";
echo "<tr><td>Codename</td><td>Location</td><td>Rating</td></tr>";



 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 echo $result['firstname']; 
 echo " "; 
 echo $result['lastname']; 
 echo "<br>"; 
 echo $result['location']; 
 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; 
 } 



 ?>
4

2 回答 2

1

您需要检查您的$_POST变量是否通过 using 设置,isset()因为当您没有发布任何内容时它们没有设置。这就是导致通知出现的原因。

因此,您需要执行以下操作:

$searching = isset($_POST['searching']) ? $_POST['searching'] : '';
$find = isset($_POST['find']) ? $_POST['find'] : '';
$field = isset($_POST['field']) ? $_POST['field'] : 
于 2014-01-09T07:55:28.293 回答
1

您可以使用isset(),但在这里,您正在使用POST方法来搜索结果,您应该在哪里使用GET所以使用GET方法而不是POST仍然,确保您也isset()使用$_GET

if(isset($_GET['searching'])) {
   //Some code
}
于 2014-01-09T07:56:50.457 回答