将搜索所有选项添加到下拉菜单中?

时间:2014-01-01 19:41:04

标签: php mysql

我希望有人可以帮助我,我敢说的是一个简单的问题,但我无法弄明白该怎么做。 我有一个表单,使用包含选项的下拉菜单搜索my​​sql数据库。我想在我的下拉菜单中添加一个“全部”选项,以便可以从数据库中提取所有结果。请参阅下面的我的脚本。

非常感谢提前。

<form action="" method ="post">

<select name="city">
      <option value="London"> London </option>
      <option value="Manchester"> Manchester </option>
      <option value="Bristol"> Bristol </option>
</select>
<select name="country">
      <option value="Australia"> Australia </option>
      <option value="England"> England </option>
      <option value="France"> France </option>
</select>
<input type="submit" name="submit" value="search">
</form>

<?php 


  // Connect to your Database 
  mysql_connect("localhost", "xxxxx", "xxxx") or die(mysql_error()); 
  mysql_select_db("xxxx") or die(mysql_error()); 


  if($_POST)
  {
  $city = $_POST['city'];

   $country = $_POST['country'];



  //connect to your database and select the appropriate database

  $query = mysql_query("SELECT * FROM members WHERE 
  city = '".$city."' AND country ='".$country."'") or die(mysql_error());

  $num = mysql_num_rows($query);

 echo "$num results found!<br>";

 while($result = mysql_fetch_assoc($query))
 {
  $username = $result['username'];
  $city = $result['city'];
  $country = $result['country'];


  echo "<b>username</b> : $username<br>

  <b>City : </b>$city <br><b>Country : </b>$country <br<br>";
   }

   }

   ?>

5 个答案:

答案 0 :(得分:0)

在下拉菜单中添加“全部”选项:

<option value="all">All</option>

并使用if来检查是否选择了“all”。如果是,则从数据库中选择SELECT *

if(check if "all" is selected) {
    // SQL to SELECT * from the database
}
else {
    // the code you have now to select one city from the DB
}

答案 1 :(得分:0)

也许是这样的:

$where = "";
  if(isset($_POST['city']) && isset($_POST['country']))
  {
   $city = $_POST['city'];
   $country = $_POST['country'];
   $where = "WHERE city = '".$city."' AND country ='".$country."'";
  }

   $query = mysql_query("SELECT * FROM members ".$where) or die(mysql_error());

答案 2 :(得分:0)

添加一个空值选项:

<option value="">All</option>

根据值构建SQL查询:

$sql = "SELECT * FROM members WHERE 1";

if(!empty($city)) $sql .= " AND city = '".$city."'";
if(!empty($country)) $sql .= " AND country = '".$country."'";

这是基本的...但请注意,您的脚本(以及此更新)容易受到SQL注入攻击,您应该使用mysql转义,更好的PDO绑定值。 Mysql_ *函数也已经过时了。你应该真的尝试深化。

答案 3 :(得分:0)

试试这个:

<form action="" method ="post">

<select name="city">
      <option value="London"> London </option>
      <option value="Manchester"> Manchester </option>
      <option value="Bristol"> Bristol </option>
      <option value="all"> All </option>
</select>
<select name="country">
      <option value="Australia"> Australia </option>
      <option value="England"> England </option>
      <option value="France"> France </option>
      <option value="all"> All </option>
</select>
<input type="submit" name="submit" value="search">
</form>

<?php


  // Connect to your Database
  mysql_connect("localhost", "xxxxx", "xxxx") or die(mysql_error());
  mysql_select_db("xxxx") or die(mysql_error());


  if($_POST)
  {
      $city = $_POST['city'];
      $country = $_POST['country'];
      $city = $city == 'all' ? '' : $city;
      $country = $country == 'all' ? '' : $country; 
      $sql = "SELECT * FROM members Where 1 ";
      if($city)
          $sql .= " AND city='{$city}' ";
      if($country)
          $sql .= " AND country='{$country}'";    
      //connect to your database and select the appropriate database

      $query = mysql_query($sql) or die(mysql_error());

      $num = mysql_num_rows($query);

      echo "$num results found!<br>";

      while($result = mysql_fetch_assoc($query))
      {
          $username = $result['username'];
          $city = $result['city'];
          $country = $result['country'];


          echo "<b>username</b> : $username<br>

  <b>City : </b>$city <br><b>Country : </b>$country <br<br>";
      }

  }

答案 4 :(得分:0)

更改您的查询:

 WHERE city LIKE '".$city."' AND country LIKE '".$country."'

 WHERE city LIKE '".$city."' AND country LIKE '".$country."'

然后添加一个选项:

<option value="%%"> All </option>

这将生成查询:

 WHERE city LIKE '%%' AND country LIKE '%%'

将返回列不是NULL的任何结果。