创建下拉列表PHP数据库搜索?

时间:2011-11-28 13:54:44

标签: php mysql phpmyadmin full-text-search

所以我正在尝试创建一个下拉列表搜索,以便在PHP和我的sql中搜索我的数据库。到目前为止,我已经能够创建一个简单的文本搜索,当我将它们输入搜索框时,它会返回所需的结果。但是我希望能够从下拉列表中选择一个选项而不是输入它。我还没有找到解决方案,并希望得到任何帮助。

这是我的search.php代码......

<?php

mysql_connect ("localhost", "root","")  or die (mysql_error());
mysql_select_db ("schoolsni");


$term = $_POST['term'];

$sql = mysql_query("SELECT * FROM  product WHERE  model LIKE '%$term%'");

while ($row = mysql_fetch_array($sql)){
    echo 'Product ID '.$row['product_id'];
    echo '<br/> Model: '.$row['model'];
    echo '<br/> quantity: '.$row['quantity'];
    echo '<br/><br/>';
    }

?>

这是我的HTML代码......

<div class="UFcol1">
    <form action="search.php" method="post">
        <select name="term">
    <option value="product">product</option>
    <option value="product1">product 1</option>
    <option value="product3">product 3</option>
    <option value="product4">product 4</option>
    </select>
        <input type="submit" name="submit" value="Submit" />
    </form>
</div>

...谢谢

3 个答案:

答案 0 :(得分:1)

<?php 
$mysqli = new mysqli( 'host', 'user', 'pass', 'table' ); 
if( $_POST['submit'] ){
   $value = $mysqli->real_escape_string( $_POST['term'] );
  $result = $mysqli->query("SELECT * FROM product WHERE model LIKE %$value%");
while( $row = $result->fetch_assoc() ){
   echo $row['field'];
}
 }else{
   $results = $mysqli->query( "SELECT DISTINCT procucts.name from products WHERE products.stock = 1" );
   $options = '<select>';
   while( $row = $results->fetch_assoc( ) ){
    $options .= "<option value='{$row['name']}'>$row['name']</option>";
   }
   $options .= '</select>';
?>

   <div class="UFcol1">
  <form action="search.php" method="post">
    <?=$options?>
      <input type="submit" name="submit" value="Submit" />
   </form>
</div>
<?php } ?>

这是快速代码,可能不是100%正确我也遗漏了检查你的连接是否成功的事情。 php.net上有很多关于此的信息和例子。

编辑:从disticnt数据库值添加自填充选项列表。

答案 1 :(得分:0)

您可以使用表格本身填充选择框:

$sql = mysql_query('SELECT model FROM  product ORDER BY model');
$models = array();
while ($row = mysql_fetch_array($sql)){
    $models[] = $row;
}
?>
<form action="search.php" method="post">
  <select name="term">
  <?php
  foreach ($models as $model) {
  ?>
    <option value="<?php echo $model['model']?>"><?php echo $model['model']?></option>
  <?php
  }
  ?>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>

要获得常见模型根的列表,您可以执行以下操作:

$sql = mysql_query('SELECT model FROM  product ORDER BY model');
$models = array();
while ($row = mysql_fetch_array($sql)){
    /*
     * Break the model name up into parts, assuming each part
     * is separated by a space. Then just use the first part
     * to build up an array of common model starts.
     * 
     * Because we're using the sub-part as a key, we'll only 
     * get a single instance of each one.
     */

    $modelParts = explode(' ', $row['model']);

    // e.g. $models['product'] = 'product'

    $models[$modelParts[0]] = $modelParts[0];
}

事实上,您可以使用它来构建一个既有完整模型名称又有部分名称的选择框:

$sql = mysql_query('SELECT model FROM  product ORDER BY model');
$models = array();
while ($row = mysql_fetch_array($sql)){
    $modelParts = explode(' ', $row['model']);
    $models[$modelParts[0]][] = $row['model'];
}

// Equivalent to ending up with something like:
// $models['product'] = array('product 1', 'product 2');

然后你可以遍历它们并输出一个这样的列表:

  <select name="term">
  <?php foreach ($models as $modelRoot => $modelEntries) { ?>
    <option value="<?php echo $modelRoot?>"><?php echo $modelRoot?></option>
    <?php foreach ($modelEntries as $model) { ?>
      <option value="<?php echo $model['model']?>"><?php echo $model['model']?></option>
    <?php } ?>
  <?php } ?>
  </select>

答案 2 :(得分:0)

<?php

mysql_connect ("localhost", "root","")  or die (mysql_error());
mysql_select_db ("schoolsni");


$term = $_POST['term'];

$sql = mysql_query("SELECT * FROM  product WHERE  model LIKE '%$term%'");
?>
<select name='term'>
<?php
while ($row = mysql_fetch_array($sql)){
?>
  <option value='<?php echo $row['product_id']?>'><?php echo $row['model'];?></option>
<?php

}
?>
</select>