如何使用下拉列表过滤SQL数据

时间:2017-10-30 20:24:24

标签: php mysql

我刚刚开始使用SQL,PHP和HTML,我打算学习一些课程,但在继续前进之前,我需要一个基本的工作模型。我有一个SQL数据库,有几个字段。我可以毫无问题地显示这些数据。我想用下拉框过滤这些数据。目前,我在下拉框中填充了Select Distinct命令。我似乎无法弄清楚修改我的SQL Select命令的语法,只显示与我的下拉框匹配的记录。哈,我是第一个承认我不知道我在做什么的人,但我的问题是我可能不知道我需要查找如何解决我的问题。谷歌和我都很紧张,但我们无法理解这一点。你能为我指出正确的方向吗?谢谢!

<!DOCTYPE html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}
td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}
tr:nth-child(even){
    background-color: #dddddd;
}
</style>

<body>
<form action='new1.php' method="get" name='fPIC'>
<select class="form-dropdown validate[required]" style="width:150px" id="input_PIC" name="sPIC">

<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");

mysqli_select_db($link,'pd4poc');

$query = "SELECT DISTINCT PIC FROM dummydata order by PIC asc";
$result = mysqli_query($link,$query);
$menu=" ";

while($row = mysqli_fetch_array($result))
    {
    $menu .= "<option value=".$row['PIC'].">" .$row['PIC']. "</option>";
    }
echo $menu;


echo "<table>";
while($row = mysqli_fetch_array($result))
    {

    echo "<tr><td>" . $row['PIC'] . "<td><td>" , $row['Reason'] . "<td><td>", $row['Status'] . "</td></tr>";
    }
echo "</table>";

 mysqli_close($link);
?>
</select>
<input type="submit" name="nPIC" value="Filter">
</form>
<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");

mysqli_select_db($link,'pd4poc');

$query = "SELECT * FROM dummydata"; //need to be picked from the pull down
$result = mysqli_query($link,$query);

echo "<table>";
echo "<tr>
        <th>PIC</th>
        <th>Reason</th>
        <th>PlanApply</th>
        <th>HOT</th>
        <th>Status</th>
        <th>Comments</th>
    </tr>\n";
while($row = mysqli_fetch_array($result))
    {
    echo "<tr><td>" . 
    $row['PIC'] . "<td>",
    $row['Reason'] . "<td>",
    $row['PlanApply'] . "<td>",
    $row['HOT'] . "<td>",
    $row['Status'] . "<td>",
    $row['Comments'] . "</td>\n</tr>";
    }
echo "</table>";

mysqli_close($link);
?>

</body>
</head>

0 个答案:

没有答案