SQL Query基于下拉菜单选择

时间:2013-04-26 01:08:53

标签: php html mysql sql phpmyadmin

我创建了一个这样的下拉列表:

<header>
    <h3 align="center"> Highway State </h3>
</header>
<section>
    <article>
        <hgroup>
        </hgroup>
        <p align="center">
        <form style="text-align:center" method="post" action="">

            <select name="dropdown1">
                <?php echo query1(); ?> 
            </select>

        <input type="submit" value="Submit" />
        </form> 
        </p>
    </article>

其中query1()是

function query1()
{
    $myHTMLData = '';
    $c=connect();
    $myData=mysqli_query($c, "SELECT DISTINCT highway_state FROM highways_highway ORDER BY highway_state");
    while($record = mysqli_fetch_array($myData))
    {
        $myHTMLData .= '<option value="' . $record['highway_state'] . '">' . $record['highway_state'] . '</option>' ;
    }
    return $myHTMLData;
}

所以我正在做的是用状态填充下拉菜单。

我现在的问题是如何进行sql查询 SELECT ID,exit_name FROM highways_exit WHERE highway_ID =(SELECT ID FROM highways_highway WHERE highway_state = * dropdown1 *

* dropdown1 * 是我希望查询基于dropdown1中的选择的地方。 我该怎么做?

另外:当用户点击提交按钮时,如何在网页上显示此查询?

我尝试解决方案(用index.php编写)

    <header>
        <h3 align="center">Exit Name</h3>
    </header>
    <section>

        <table border='1' align="center">
            <tr>
            <th>Exit Name</th>
            <th>Exit ID</th>
            </tr>
<?php   
        $c=connect();
        $hstate = $_POST['dropdown1'];
        $hname = $_POST['dropdown2'];
        $hdir = $_POST['dropdown2pt5'];

        $result=mysqli_query($c, "SELECT exit_name, id FROM highways_exit WHERE highway_ID = (SELECT id FROM highways_highway WHERE highway_state = ('" . $hstate . "') AND highway_name = ('" . $hname . "') AND highway_dir = ('" . $hdir . "')) ");  
            while($row = mysqli_fetch_array($result))
            {
                $arrexit = $row['exit_name'];
                $arrid = $row['id'];
                echo "<tr><td>$arrexit</td><td>$arrid</td></tr>";
            }
  ?>
        </table>

    </section>
    <footer>
        <f1 align="center"></f1>
    </footer>

使用菜单项是否正确?这个和菜单对象都在index.php上,有没有办法让它工作,以便它将接受菜单项并在刷新/提交时产生这个表?

1 个答案:

答案 0 :(得分:2)

您可以使用$_POST['dropdown1']获取价值 然后,您可以使用它来查询数据库。