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

时间:2018-01-18 09:53:59

标签: php mysql

我正在尝试搜索功能。但我没有在特定日期进行搜索,而是尝试从一系列日期进行搜索,以便只显示我想要的数据。

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

 ?>

3 个答案:

答案 0 :(得分:0)

您需要像这样执行查询

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

答案 1 :(得分:0)

您的查询必须是(即从lowerdateupperdate之间的表格中选择*):

此处下限为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')

这必须有效。

答案 2 :(得分:0)

您需要使用$ _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");