嗨我的日期范围搜索有问题导致它获取错误的结果
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 <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;
}
感谢
答案 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
替换所有出现的
SalesDate
至UNIX_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"]))