0

hi im having trouble in my date range search cause it fetch wrong result

ex: im searching 04/01/2013 to 04/02/2013 supposedly that the result will show the data from 04/01/2013 to 04/02/2013 these dates but it fetch it wrong data

this is my form

<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>

and here is my search query

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;
                                }

thanks

seven
  • 115
  • 1
  • 1
  • 9

1 Answers1

0

Change

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

To

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

The name of the datepicker should be end as you have used $_REQUEST["end"] in the date receiving end.

EDIT

Use this query

$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;

If you are using GROUP BY on salesDate field use it this way GROUP BY UNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y'))

EDIT 2

Substitute all occurance of

SalesDate to UNIX_TIMESTAMP(str_to_date(SalesDate, '%d/%m/%Y'))
and
$_REQUEST["startfrom"] to
strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))
and
$_REQUEST["end"] to
strtotime(mysql_real_escape_string($_REQUEST["startfrom"]))

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • thanks an i edited it no but there is a another problem my SalesDate is varchar how can i convert it datetime or date? – seven Sep 11 '13 at 01:12
  • here it is `$sql = "SELECT CategoryID,OracleCategory,str_to_date(SalesDate, '%d/%m/%Y'),OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,SUM(NetPrice) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE str_to_date(SalesDate, '%d/%m/%Y') >= '".date('d-m-Y',strtotime(mysql_real_escape_string($_REQUEST["startfrom"])))."' AND str_to_date(SalesDate, '%d/%m/%Y')<='".date('d-m-Y',strtotime(mysql_real_escape_string($_REQUEST["end"])))."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby;` – seven Sep 11 '13 at 02:31
  • i also change this `if (date('d-m-Y',strtotime(mysql_real_escape_string($_REQUEST["startfrom"])))<>'' and date('d-m-Y',strtotime(mysql_real_escape_string($_REQUEST["end"])))<>'') {` as you said – seven Sep 11 '13 at 02:32
  • Sorry change `date('d-m-Y',..)` to `date('d/m/Y'..)` and What is on `$search_groupby` ? – Deepak Sep 11 '13 at 02:40
  • here `$search_groupby = "GROUP BY CategoryID,OracleCategory,ProductCode ORDER BY CategoryID,OracleCategory,ProductCode ";` – seven Sep 11 '13 at 02:50
  • still doesn't fetch any data – seven Sep 11 '13 at 02:52
  • Check my edit now and also what is the format of date stored in your database in terms of %d %m %y? – Deepak Sep 11 '13 at 03:01
  • it is varchar and it stores the value like this 04/01/2013 – seven Sep 11 '13 at 03:24