如何根据下拉菜单值显示特定的数据行?

时间:2018-12-01 10:02:00

标签: php mysql sql database

正如标题所说,我想根据用户对年份的选择显示特定的数据行。

如果用户选择了2017,则我只想显示在2017年创建的数据行。

我知道查询有问题,我试图了解如何编写查询。谁能帮帮我吗?预先谢谢你!

<form action="Report1.php" method="POST">
    Select Year:<select name="sortyear" id="sortyear">
        <option>2017</option>
        <option>2018</option>
    </select>
    <?php
        $sortyear = $_POST['sortyear'];
        $db = mysqli_connect('localhost','root','','customercaremodule');
        date_default_timezone_set('Asia/Kuala_Lumpur');
        if ($db->connect_error) {
            die("Connection failed: " . $db->connect_error);
        }
        $sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
        $result = mysqli_query($db,$sql);
    ?>
</form>

3 个答案:

答案 0 :(得分:0)

忽略安全漏洞,您可以执行以下操作:

<?php $getYears = sqlquery() ?>
<form action="Report1.php" method="POST">
<select name="sortyear" id="sortyear">
 <?php for ($i = 0; $i < $getYears.length; $i++): ?>
 <option value="<?php echo $getYears[$i];?>"><?php echo $getYears[$i];?></option>
 <?php endif; ?>
</select>

你可以

<?php for () {
}?>

但是for:允许您返回html代码并突出显示语法;)

答案 1 :(得分:0)

我设法通过添加按钮

解决了该问题
<input type ="submit" name="search" class="btn btn-sm btn-primary" value="Filter" style="background-color: #474747;padding: 5px 5px !important;margin-bottom:5px;">

并以此方式编码php

        if(isset($_POST['search']))
        {

                      $db = mysqli_connect('localhost','root','','customercaremodule');
                      date_default_timezone_set('Asia/Kuala_Lumpur');
                      if ($db->connect_error) {
                          die("Connection failed: " . $db->connect_error);
                      }
            $sortyear = $_POST['sortyear'];
        $sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
        $result = mysqli_query($db,$sql);
        }

答案 2 :(得分:0)

您可以尝试以下方法:

<form action="Report1.php" method="POST">
    Select Year:<select name="sortyear" id="sortyear">
        <option>2017</option>
        <option>2018</option>
    </select>
    <input type="submit" name="submit" value="Get Annual Data" />
    <?php
        if(isset($_POST['submit'])){
            $sortyear = $_POST['sortyear'];
            $db = mysqli_connect('localhost','root','','customercaremodule');
            date_default_timezone_set('Asia/Kuala_Lumpur');
            if ($db->connect_error) {
                die("Connection failed: " . $db->connect_error);
            }
            $sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = " .$sortyear .")";
            $result = mysqli_query($db,$sql);
        }
    ?>
</form>

请注意,这主要是您的代码,我只是对此artcile进行了相应的修改。关键是我添加了按钮,使用户可以提交表单。