PHP 数据库搜索表单出现问题


Trouble With PHP Database Search Form

我创建了一个包含五个字段的数据库

ValueA
ValueB
ValueC
ValueD
ValueE

我正在尝试制作一个搜索表单,可以按这些单独的字段中的每一个进行搜索,例如,如果 ValueB 中的值为"蓝色",请从下拉列表中选择 ValueB,然后输入"蓝色"以打印出蓝色所属行中的所有值。到目前为止,我已经创建了一个名为"findme.html"的html文件:

<html>
<head>
<title>Search</title>
</head>
<body bgcolor=#ffffff>
<h2>Search</h2>
<form name="search" method="post" action="findme2.php">  
Search for: <input type="text" name="find" /> in  
<Select NAME="field"> 
<Option VALUE="ValueA">Value A</option> 
<Option VALUE="ValueB">Value B</option> 
<Option VALUE="ValueC">Value C</option>
<Option VALUE="ValueD">Value D</option>
<Option VALUE="ValueE">Value E</option> 
</Select> 
<input type="submit" name="search" value="Search" /> 
</form>
</body>
</html>

并且还创建了一个名为"FindMe2.php"的PHP文件:

<html>
<head>
<title>Searching through Database Table mytablename</title>
</head>
<body bgcolor=#ffffff>

<?php
include "config.php";
echo "<h2>Search Results:</h2><p>";
if(isset($_POST['search']))            
{
$find =$_POST['find'];
}
//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
$username="xxxxxxxx";
$password="xxxxxxxx";
$database="xxxxxx_xxxxxxx";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// We perform 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
$iname = mysql_query("SELECT * FROM mytablename WHERE upper($field) LIKE '%$find%'")
or die(mysql_error());
//And we display the results
while($result = mysql_fetch_array( $iname ))
{
echo "id :" .$result['ValueA'];
echo "<br> ";
echo "name :".$result['ValueB'];
echo "<br>";
echo "name :".$result['ValueC'];
echo "<br>";
echo "name :".$result['ValueD'];
echo "<br>";
echo "name :".$result['ValueE'];
echo "<br>";
echo "<br>";
}
$anymatches = mysql_num_rows($iname);
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;

?> 
</body>
</html>

我相信我的问题出在查询命令上,但我不确定如何调整语法。谁能帮我?

您忘了设置$field变量。

在您的 if 语句中,您应该将其更改为

if(isset($_POST['search']))            
{
$find =$_POST['find'];
$field =$_POST['field'];
}

然后它应该可以工作。