创建一个过滤器按钮SQL查询

时间:2017-02-12 19:09:41

标签: php mysql filter

我在SQL查询上创建了一个过滤器按钮。我有一个表单,用户可以在其中插入日期,然后按一个按钮。每当用户单击此按钮时,它仅在表中搜索该日期。我创造了所有这些,但它没有奏效。

它没有给我一个错误或任何东西..

$mysqli = mysqli_connect("localhost", "root", "", "urenregistratie");

按钮没有做任何事情。这就是我所拥有的:

<body>
   <div class="form">
   <p>
      <a href="dashboard.php">Dashboard</a> | <a href="view.php">Projecten inzien</a> | <a href="logout.php">Loguit</a>
   </p>
   <h2>Projecten:</h2>
   <table width="100%" border="1" style="border-collapse:collapse;">
      <thead>
         <tr>
            <th><a href="overzicht.php?sort=gebruiker">Gebruiker</a></th>
            <th><a href="overzicht.php?sort=projectname">Project naam:</a></th>
            <th><a href="overzicht.php?sort=aantaluren">Aantal uren</a></th>
            <th><a href="overzicht.php?sort=datum">Datum:</a></th>
      </thead>
      <tbody>
         <?php
            //SORT BY ASC OR DESC

            $sel_query="SELECT * FROM uren";

            if ($_GET['sort'] == 'gebruiker') {
                $sel_query .= " ORDER BY gebruiker";
            }elseif ($_GET['sort'] == 'projectname') {
                $sel_query .= " ORDER BY projectname";
            }elseif ($_GET['sort'] == 'aantaluren') {
                $sel_query .= " ORDER BY aantaluren DESC";
            }elseif ($_GET['sort'] == 'datum') {
                $sel_query .= " ORDER BY datum";
            }

            $result = mysqli_query($mysqli,$sel_query);
            while($row = mysqli_fetch_assoc($result)) {
            ?>
         <tr>
            <td align="center"><?php echo $row["gebruiker"]; ?></td>
            <td align="center"><?php echo $row["projectname"]; ?></td>
            <td align="center"><?php echo $row["aantaluren"]; ?></td>
            <td align="center"><?php echo $row["datum"]; ?></td>
         </tr>
         <?php } ?>
      </tbody>
   </table>
   <form name="filter" method="POST" action="overzicht.php">
      "<input type="hidden" name="uid" value="<?php echo $uid; ?>">
      SHOW POSTS FROM:
      <input type"date" name="date">
      <input type="submit" name="submit" value="Filter">
   </form>
   <?php
      //FILTER BUTTON

      if (isset($_POST['submit'])) {
        $uid = $_POST['uid'];
        $date = $_POST['date'];
        $filter = "WHERE DATUM(post_date) IS '$date'";
        $query = "SELECT * FROM uren $filter";
        $respost = mysqli_query($mysqli, $query);
      } else {
        $query = "SELECT * FROM uren";
        $respost = mysqli_query($mysqli, $query);
      }
      ?>
</body>

1 个答案:

答案 0 :(得分:0)

action移除formfilter查询未在任何地方使用

 <form name="filter" method="POST" action="">
            <input type="hidden" name="uid" value="<?php echo $uid; ?>">
            SHOW POSTS FROM:
            <input type"date" name="date">
            <input type="submit" name="submit" value="Filter">
        </form>
        <?php

        //FILTER BUTTON

        if (isset($_POST['submit'])) {
            $uid = $_POST['uid'];
            $date = $_POST['date'];

            $query = "SELECT * FROM uren WHERE DATUM(post_date)='$date'";
            $respost = mysqli_query($mysqli, $query);
        } else {
            $query = "SELECT * FROM uren";
            $respost = mysqli_query($mysqli, $query);
        }
        ?>

我认为这就是你的代码的含义......