在两个不同的日期之间搜索

w1e3prcc  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(370)

这个问题在这里已经有答案了

如何使用mysql在两个日期之间进行查询(10个答案)
两年前关门了。
我正在尝试一个搜索功能。但是我不是从特定的日期开始搜索,而是尝试从一个日期范围进行搜索,以便它只显示我想要的数据。

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

 ?>
6jjcrrmo

6jjcrrmo1#

您需要使用$\u post从标题中获取参数。
试试下面这个。
更多信息,请参考这里

$start = $_POST['start'];
$end = $_POST['end'];

$query = $mysqli->query("SELECT * FROM submission WHERE date BETWEEN '$start' AND '$end'") or die ("Could not search");
zpjtge22

zpjtge222#

您的查询必须是(即,select*from table between lowerdate 以及 upperdate ):
下面的日期是2017-12-26 10:37:45,上面的日期是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')

这必须奏效。

cyej8jka

cyej8jka3#

您需要像这样执行查询

$startDate="2017-07-23";
$endDate="2018-01-01";
     $query = $mysqli->query("SELECT * FROM submission
     WHERE date BETWEEN '".$startDate."' AND '".$endDate."'")
        ;

相关问题