在两个不同的日期之间搜索[重复]
Posted
技术标签:
【中文标题】在两个不同的日期之间搜索[重复]【英文标题】:Search in between two different dates [duplicate] 【发布时间】:2018-06-27 08:25:21 【问题描述】:我正在尝试搜索功能。但我不是从特定日期搜索,而是尝试从一个日期范围搜索,以便它只显示我想要的数据。
<form action ="searchreceipt.php" method ="post">
<input name="start" type="date" size="30" required />
<input name="end" type="date" size="30" required />
<input type="submit" value="Search"/>
</form>
<?php
$output = '';
if(isset($_POST['search']))
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$query = $mysqli->query("SELECT * FROM submission WHERE date BETWEEN 'start' AND 'end'") or die ("Could not search");
while ($row = $query->fetch_array(MYSQLI_ASSOC))
$officer_id = $row ['officer_id'];
$sbranch_no = $row ['sbranch_no'];
$carno = $row ['carno'];
$cost = $row ['cost'];
$area = $row ['area'];
$receipt = $row ['receipt'];
echo "<table border='1' style='width:50%'>";
echo "<td>";
echo "<b>Receipt ID: <a href ='transactiondetail.php?receipt=$receipt'>$receipt</b></a>";
echo "<br><br>";
echo "Used By: $officer_id";
echo "<br><br>";
echo "Officer Branch No: $sbranch_no";
echo "<br><br>";
echo "Cost: $cost";
echo "<br><br>";
echo "Area travelled: $area";
echo "<br><br>";
echo "</td>";
echo "</table>";
?>
【问题讨论】:
date
列是什么类型的?您遇到什么错误?
"SELECT * FROM submission WHERE date BETWEEN 'start' AND 'end'"
,我认为这是错误的。看起来你只是使用 start 和 end 作为字符串
开始是一个字符串
查看带有绑定参数的预处理语句
【参考方案1】:
你需要像这样执行查询
$startDate="2017-07-23";
$endDate="2018-01-01";
$query = $mysqli->query("SELECT * FROM submission
WHERE date BETWEEN '".$startDate."' AND '".$endDate."'")
;
【讨论】:
没有。查看准备好的和绑定的查询【参考方案2】:您的查询必须是(即从lowerdate
和upperdate
之间的表中选择*):
这里的lowerdate是2017-12-26 10:37:45,upperdate是2017-12-27 09:38:37
SELECT * FROM `table_name` WHERE (field_name BETWEEN '2017-12-26 10:37:45' AND '2017-12-27 09:38:37')
这一定行得通。
【讨论】:
【参考方案3】:您需要使用 $_POST 从标头中获取参数。
在下面试试这个。
更多内容可以参考here
$start = $_POST['start'];
$end = $_POST['end'];
$query = $mysqli->query("SELECT * FROM submission WHERE date BETWEEN '$start' AND '$end'") or die ("Could not search");
【讨论】:
查看准备好的语句以上是关于在两个不同的日期之间搜索[重复]的主要内容,如果未能解决你的问题,请参考以下文章