我得到了这个用于搜索的代码,但我遇到了问题。它运行良好,但在我单击搜索选项卡后,它显示错误。但是,如果我尝试输入查询进行搜索,它工作正常。
这些是错误:
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;
}
?>