0

我有一个包含 3 个字段的搜索区域,一个是输入字段,另外两个是下拉菜单。有人在输入字段中输入内容,或者他们从两个下拉菜单(州和城市)中进行选择。

输入搜索工作正常。选择菜单没有。

这是表格。

<form name="ffl_finder" method="get" action="index.php">
  Enter a Zip Code to find a Firearms Dealer near you: &nbsp;<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());
    } ?>

我需要查询来搜索城市和州,但我似乎无法获得正确的条件。我知道这是很多代码,但是如果有人可以提供帮助,那就太好了。

谢谢,

克林特

4

1 回答 1

0
<?php
        while ($rrows2 = mysql_fetch_array($rs_results2)) 
        {
    ?>
        <option value="<?php echo $rrows2['state']; ?>"><?php echo $rrows2['state']; ?></option>
    <?php
        }
    ?>

state并且city可能是 ffl_list 表的 cols 名称,您必须使用字符串作为从mysql_Fetch_array. 你做错$rrows2$rrows3

当您为查询创建过滤器时,您设置了一个硬字符串,而不是 GETted 数据:

  $cond2 = '';
  if($_GET['qoption2'] != '') {
     $cond2 = "`state` = '".$_GET['qoption2']."'";
  }
于 2013-05-12T01:38:29.457 回答