【问题标题】:Search data table with Date使用日期搜索数据表
【发布时间】:2013-11-28 02:09:46
【问题描述】:

我有一个数据表,它是一个表单提交的结果,表单有 start_date 和 end_date 类型为DATETIME-LOCAL

在我的数据库中,它的 DATETIME(不显示上午和下午),
我想根据日期而不是日期和时间向我的数据表添加搜索功能,

其他类型的基于 TYPE 和 NAME 的搜索正在工作,但日期不显示,也不显示错误和结果。

<form action="search.php" id="searchform" method="POST" class="searchbox-container">
    <input type="text" id="searchbox" placeholder="Search" name="searchbox" class="searchbox" />
    <input type="date" name="date">

    <select name="select" id="select">
        <option value="type">Type</option>
        <option value="name">Name</option>
    </select>
    <input type="submit" name="search" class="searchbox-btn" value="Go" />

</form>

<?php

    if(isset($_POST['search'])){
        $search=preg_replace('#[^a-z 0-9?!]#i','',$_POST['searchbox']);


        $user="admin";
        $pass="neehahs";
        $host="localhost";
        $db_name="eventregisteration";

        $con=mysqli_connect($host, $user, $pass, $db_name);
        if(mysqli_connect_errno($con)){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $date=$_POST['date'];


        if($_POST['select']=="type"){
            $sqlcommand="SELECT * FROM eventform WHERE event_type LIKE '%$search%'";    


        }
        elseif($_POST['select']=="name"){
            $sqlcommand="SELECT * FROM eventform WHERE event_name LIKE '%$search%'";        


        }
        else {
            if($date!=0)
                $sqlcommand="SELECT * FROM eventform WHERE start_date='$date' ORDER BY start_date DESC";    


        }
        $sqldata=mysqli_query($con,$sqlcommand)
            or die("Error Getting Data");

        $count=mysqli_num_rows($sqldata);
        if($count!=0){

            echo "<table border=2 bordercolor=#440000 cellpadding=2 bgcolor=#DDDDDD width=100%>";
            echo "<tr bgcolor=#555555 style=font-size:18px align=center><th>Event Code</th><th>Event Name</th><th>Event Type</th><th>Event Level</th><th>Start Date</th>
      <th>End Date</th><th>Point</th><th>Picture</th><th>Video</th><th>Description</th></tr>";
            while($row=mysqli_fetch_array($sqldata)){
                echo "<tr align=center><td>";
                echo $row['event_code'];

                echo "</td><td>";
                echo $row['event_name'];
                echo "</td><td>";

                echo $row['event_type'];
                echo "</td><td>";

                echo $row['event_level'];
                echo "</td><td>";

                echo $row['start_date'];
                echo "</td><td>";

                echo $row['end_date'];
                echo "</td><td>";

                echo $row['points'];
                echo "</td><td>";

                echo "<a href=http://localhost/greenstudio/".$row['pic'].">".$row['pic']."</a>";
                echo "</td><td>";

                echo "<a href=http://localhost/greenstudio/".$row['video'].">".$row['video']."</a>";
                echo "</td><td>";

                echo $row['description'];
                echo "</td></tr>";

            }
            echo "</table>";

        }else{
            echo "hi";
            $search_output="<hr/>0 Results for<strong>$search</strong><hr/>";

        }
    }

?>

【问题讨论】:

    标签: php mysql date search html-table


    【解决方案1】:

    以索引友好的方式编写的查询可能如下所示

    SELECT * 
      FROM eventform
     WHERE start_date >= '$date'
       AND start_date < '$date' + INTERVAL 1 DAY
     ORDER BY start_date DESC
    

    其中$date 应为YYYY-MM-DD 格式(例如2013-11-27

    这里是SQLFiddle演示

    附带说明:既然您已经在使用mysqli,请考虑学习和使用prepared statements,而不是插入查询字符串,使其对sql 注入开放。

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多