日期范围搜索

时间:2013-09-11 00:51:27

标签: php mysql sql search date-range

嗨我的日期范围搜索有问题导致它获取错误的结果

ex:im搜索04/01/2013到04/02/2013据说结果将显示从2013年4月1日到2013年2月4日这些日期的数据,但是它获取错误的数据

这是我的表格

<form  method="post"id="myform" action="index.php" >
        <label for="from">Sales Date from :</label>
        <input name="startfrom" type="text" id="startfrom" class="datepicker form-control"/>
        <label for="to">Sales Date to :</label>
        <input name="to" type="text" id="end" class="datepicker form-control"/>
        <label>Outlet Name:</label>
        <select name="OutletName" class="input-sm form-control">
            <option value="">--</option>
            <?php
            error_reporting(0);
            include 'config.php';
                $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY OutletName ORDER BY OutletName";
                $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
                while ($row = mysql_fetch_assoc($sql_result)) {
                    echo "<option value='".$row["OutletName"]."'".($row["OutletName"]==$_REQUEST["OutletName"] ? " selected" : "").">".$row["OutletName"]."</option>";
                }
            ?>
        </select>
        <label>Category:</label>
        <select name="Category" class="input-sm form-control ">
            <option value="">--</option>
            <?php
            error_reporting(0);
            $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY Category ORDER BY Category";
                $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
                while ($row = mysql_fetch_assoc($sql_result)) {
                    echo "<option value='".$row["Category"]."'".($row["Category"]==$_REQUEST["Category"] ? " selected" : "").">".$row["Category"]."</option>";
                }
            ?>
        </select></br>
        <label>Product Code:</label>
        <select name="ProductCode" class="input-sm form-control ">
            <option value="">--</option>
            <?php
            error_reporting(0);
            $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY ProductCode ORDER BY ProductCode";
                $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
                while ($row = mysql_fetch_assoc($sql_result)) {
                    echo "<option value='".$row["ProductCode"]."'".($row["ProductCode"]==$_REQUEST["ProductCode"] ? " selected" : "").">".$row["ProductCode"]."</option>";
                }
            ?>
        </select></br>
        <button type="submit" class="btn  btn-primary btn-block" name="submit" id="submit"  />FIND&nbsp;<span class="glyphicon glyphicon-search"></span></button>

这是我的搜索查询

if ($_REQUEST["Category"]<>'') {
                                    $search_Category = " AND Category='".mysql_real_escape_string($_REQUEST["Category"])."'";   
                                }
                                if ($_REQUEST["ProductCode"]<>'') {
                                    $search_ProductCode = " AND ProductCode='".mysql_real_escape_string($_REQUEST["ProductCode"])."'";  
                                }
                                if ($_REQUEST["OutletName"]<>'') {
                                    $search_OutletName = " AND OutletName='".mysql_real_escape_string($_REQUEST["OutletName"])."'"; 
                                }

                                $search_groupby = "GROUP BY CategoryID,OracleCategory,ProductCode ORDER BY CategoryID,OracleCategory,ProductCode ";

                                if ($_REQUEST["startfrom"]<>'' and $_REQUEST["end"]<>'') {
                                    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE SalesDate >= '".mysql_real_escape_string($_REQUEST["startfrom"])."'AND SalesDate <='".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode;
                                } else if ($_REQUEST["startfrom"]<>'') {
                                    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE SalesDate >= '".mysql_real_escape_string($_REQUEST["startfrom"])."'".$search_OutletName.$search_Category.$search_ProductCode;
                                } else if ($_REQUEST["end"]<>'') {
                                    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE SalesDate <= '".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode;
                                }else {
                                    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE ECRNo>0 ".$search_OutletName.$search_Category.$search_ProductCode;
                                }

感谢

1 个答案:

答案 0 :(得分:0)

更改

<input name="to" type="text" id="end" class="datepicker form-control"/>

<input name="end" type="text" id="end" class="datepicker form-control"/>

datepicker的名称应为end,因为您在日期接收结束时使用了$_REQUEST["end"]

修改

使用此查询

$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE UNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y')) >= '".strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))."' AND UNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y'))<='".strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))."'".$search_OutletName.$search_Category.$search_ProductCode;

如果您在salesDate字段上使用GROUP BY,请使用GROUP BY UNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y'))

编辑2

替换所有出现的

SalesDateUNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y'))

$_REQUEST["startfrom"]
 strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))

$_REQUEST["end"]
strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))