使用下拉菜单中的值来搜索sql数据库

时间:2013-02-09 05:45:52

标签: php sql drop-down-menu search-engine

我是php的新手,想问一下这个代码..我有一个下拉菜单和一个按钮.. 我想在sql数据库中搜索我在那些下拉菜单中选择的内容.. 使用下拉菜单,sql数据库中搜索项的sql语法是什么? 默认情况下,我写 - > SELECT * FROM helpline

应该是 - > SELECT * FROM helpline WHERE MISC = %**item in drop down menu**%

这是我的数据库=帮助台

Table = helpline

NAME  |    DATE    |  MISC  |
John  | 02/01/2011 | Item 1 |
Mark  | 03/01/2011 | Item 2 |

这是我的代码

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<label for="namelist"></label>
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="submit" name="show" id="show" value="Submit" />
<?php
mysql_select_db("helpdesk",mysql_connect("localhost","root",""))or die (mysql_error());
$query = "SELECT * FROM helpline"; */ This line should select what I choose in drop down menu  /*
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
?>
<table border="0" cellpadding="6" cellspacing="6" class="curve">
<thead>
<tr>
<th> <div align="right"><span class="font">NAME</span></div></th>
<th> <div align="right"><span class="font">DATE</span></div></th>
</tr>
</thead>
<tbody>   
<tr>
<th><div align="left"><span class="font"><?php echo $row['name']; ?></span></div></th>
<td><div align="left"><span class="font"><?php echo $row['date']; ?></strong></span></div></td>   
</tr>
</tbody>
&nbsp;
<?php
}
?>
</table>
</form>

因此,当我点击按钮时,它应该根据我在下拉菜单中选择的内容显示sql数据库中的所有项目。

3 个答案:

答案 0 :(得分:1)

您可以通过PHP's Online Manual中所述的php $_POST[]关联数组访问您发布的表单数据。从中您可以选择namelist并将值传递给SQL。


$query = "SELECT * FROM helpline";
if(isset($_POST['namelist'])){
   $dropdown_val = $_POST['namelist'];
   $query .= " WHERE MISC = '$dropdown_val'"; 
}

但你真的应该使用ajax,jQuery将是一个很棒的javascript框架来帮助你实现这一目标。此外,自PHP 5.5.0起,不推荐使用mysql_ *函数,将来将删除它们。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 1 :(得分:1)

没有页面刷新你可以这样做

<script>
    $(document).ready(function(){
        $("#namelist").change(function(){
            var data = $(this).val();
            $.ajax({
                type:'POST',
                data:'search_value='+data,
                url:'search_process.php',
                success:function(data){
                    $("#result").html(data);
                }               
            });         
        }); 

    });


</script>

HTML部分

<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
<input type="button" name="show" id="show" value="Submit" />

<div id="result"></div>

search_process.php页面

<?php

 // DATABASE CONNECTIVITY

 if(isset($_POST['search_value'])) {
    $val = $_POST['search_value'];


    $query = mysql_query("SELECT * FROM helpline WHERE MISC LIKE %$val%");

    // NOW RUN YOUR QUERY

    $result = '<table border="0" cellpadding="6" cellspacing="6" class="curve">';
    $result .= '<thead>';
    $result .= '<tr>';
    $result .= '<th> <div align="right"><span class="font">NAME</span></div></th>';
    $result .= '<th> <div align="right"><span class="font">DATE</span></div></th>';
    $result .= '</tr>';
    $result .= '</thead>';
while($row = mysql_fetch_array($query)){

    $result .= '<tr>';
    $result .= '<th><div align="left"><span class="font">'.$row['name'].'</span></div></th>';
    $result .= '<td><div align="left"><span class="font">'.$row['date'].'</strong></span></div></td>'; 
    $result .= '</tr>';

}
    $result .= '</table>';
    echo $result;
 }
?>

希望这能给出一个想法

答案 2 :(得分:1)

您的选择选项没有值属性

your dropdown
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled">PLEASE CHOOSE ONE ITEM:-</option>
<option>Item 1</option>
<option>Item 2</option>
</select>

change it :
<select name="namelist" id="namelist">
<option selected="selected" disabled="disabled" value="">PLEASE CHOOSE ONE ITEM:-</option>
<option value="Item 1">Item 1</option>
<option value="Item 2">Item 2</option>
</select>

提交从$ _post [&#34;名单&#34;]

获取价值后