我有一个包含3个字段的搜索区域,一个是输入字段,另外两个是下拉菜单。有人在输入字段中输入内容,或者他们从下拉菜单(州和城市)中选择。
输入搜索工作正常。选择菜单没有。
这是表格。
<form name="ffl_finder" method="get" action="index.php">
Enter a Zip Code to find a Firearms Dealer near you: <br />
<input name="q" type="text" id="q" size="40">
Select a State AND a City<br />
<select name="qoption2">
<option value="" selected="selected">Select . . .</option>
<?php
$sql2 = "SELECT DISTINCT state from ffl_list order by state asc";
$rs_results2 = mysql_query($sql2) or die(mysql_error());
while ($rrows2 = mysql_fetch_array($rs_results2)) {
?>
<option value="<?php echo $rrows2[state]; ?>"><?php echo $rrows2[state]; ?></option>
<?php
}
?>
</select>
<select name="qoption3">
<option value="" selected="selected">Select . . .</option>
<?php
$sql3 = "SELECT DISTINCT city from ffl_list order by city asc";
$rs_results3 = mysql_query($sql3) or die(mysql_error());
while ($rrows3 = mysql_fetch_array($rs_results3)) {
?>
<option value="<?php echo $rrows3[city]; ?>"><?php echo $rrows3[city]; ?></option>
<?php
}
?>
</select>
<input name="doSearch" type="submit" id="doSearch" value="Search">
</form>
以下是查询:
<?php if ($get['doSearch'] == 'Search') {
//$cond2 = '';
if($get['qoption2'] != '') {
$cond2 = "`state` = 'qoption2'";
}
//$cond3 = '';
if($get['qoption3'] != '') {
$cond3 = "`city` = 'qoption3'";
}
if($get['q'] == '') {
$sql = "select * from ffl_list where $cond2 and $cond3 order by state asc, city asc";
}
else {
$sql = "select * from ffl_list where `zip` = '$_REQUEST[q]' ORDER BY `id` asc";
}
$rs_results = mysql_query($sql) or die(mysql_error());
} ?>
我需要查询来搜索城市和州,我似乎无法获得正确的条件。我知道这是很多代码,但如果有人可以提供帮助那就太棒了。
谢谢,
奇
答案 0 :(得分:0)
<?php
while ($rrows2 = mysql_fetch_array($rs_results2))
{
?>
<option value="<?php echo $rrows2['state']; ?>"><?php echo $rrows2['state']; ?></option>
<?php
}
?>
state
和city
可能是cols'ffl_list表的名称,您必须使用字符串mysql_Fetch_array
返回的数组的键。你对$rrows2
和$rrows3
为查询创建过滤器时,设置硬字符串,而不是GETted数据:
$cond2 = '';
if($_GET['qoption2'] != '') {
$cond2 = "`state` = '".$_GET['qoption2']."'";
}