我有多字段数据库,一些变量是常量,它们是使用下拉菜单选择的,一些变量是免费输入的。我想搜索数据库并找到包含所选术语组合的行数,它在大多数情况下都有效,但如果我使用超过 2 个下拉菜单进行选择,它似乎停止工作:表单搜索:
<form action="search2.php" method="post">
<p>
Name: <input type="text" name="term1" style="background-color:#FFFF11">
<br />
<br />
Dept.: <input type="text" name="term4" style="background-color:#FFFF11">
<br />
<br />
Month: <select name="term2" style="width:65px; color: black;background-color:#FFFF11">
<option value="" style="background-color: #FFFF11;">...</option>
<option value="Jan" style="background-color: #FFFF11;" >Jan</option>
<option value="Feb" style="background-color: #FFFF11;">Feb</option>
<option value="Mar" style="background-color: #FFFF11;">Mar</option>
<option value="Apr" style="background-color: #FFFF11;" >Apr</option>
<option value="May" style="background-color: #FFFF11;">May</option>
<option value="Jun" style="background-color: #FFFF11;">Jun</option>
<option value="Jul" style="background-color: #FFFF11;" >Jul</option>
<option value="Aug" style="background-color: #FFFF11;">Aug</option>
<option value="Sep" style="background-color: #FFFF11;">Sep</option>
<option value="Oct" style="background-color: #FFFF11;" >Oct</option>
<option value="Nov" style="background-color: #FFFF11;">Nov</option>
<option value="Dec" style="background-color: #FFFF11;">Dec</option>
</select>
<br />
<br />
Year: <select name="term3" style="width:65px; color: black;background-color:#FFFF11">
<option value="" style="background-color: #FFFF11;">...</option>
<option value="2010" style="background-color: #FFFF11;" >2010</option>
<option value="2011" style="background-color: #FFFF11;">2011</option>
<option value="2012" style="background-color: #FFFF11;">2012</option>
</select>
<br />
<br />
</p>
<input class="button" type="submit" name="submit" value="Submit">
</form>
搜索代码:
<?php
include "db.inc.php";
$term1 = $_POST['term1'];
$term2 = $_POST['term2'];
$term3 = $_POST['term3'];
$term4 = $_POST['term4'];
$sql ="SELECT * FROM fixes WHERE tech LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'";
$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM fixes WHERE tech LIKE '%$term1%' AND date LIKE '%$term2%' AND date LIKE '%$term3%' AND dept LIKE '%$term4%'");
$number=mysql_num_rows($query);
?>
如果我决定对部门使用下拉菜单,使用上面相同的格式它会停止工作,也许有更好的方法吗?谢谢