如何显示搜索日期之间的记录?

时间:2019-04-18 12:06:18

标签: php mysql

它仅显示“未找到记录”,但我正在尝试使用搜索显示日期之间的记录

<?php

$query = "SELECT title, count(title) as totalnotary , notary_date, book_no 
          FROM tbl_notary ";
// Date filter
if (isset($_POST['search'])) {
    $fromDate = $_POST['fromDate'];
    $endDate  = $_POST['endDate'];

    if (!empty($fromDate) && !empty($endDate)) {
        $query = "SELECT title, count(title) as totalnotary , 
                         notary_date, book_no 
                  FROM tbl_notary 
                  Where 1 
                  and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
    }
}

// Sort
$query .= " group by book_no,title,Year(notary_date),
                     month(notary_date),day(notary_date) 
            ORDER BY notary_date DESC";
$Records = mysqli_query($conn, $query);

// Check records found or not
if (mysqli_num_rows($Records) > 0) {
    while ($Record = mysqli_fetch_assoc($Records)) {
        $book_no     = $Record['book_no'];
        $title       = $Record['title'];
        $totalnotary = $Record['totalnotary'];
        $notary_date = date("F j,Y", strtotime($Record['notary_date']));

        echo "<tr>";
        echo "<td>" . $book_no . "</td>";
        echo "<td>" . $title . "</td>";
        echo "<td>" . $totalnotary . "</td>";
        echo "<td>" . $notary_date . "</td>";
        echo "</tr>";
    }
} else {
    echo "<tr>";
    echo "<td colspan='4'>No record found.</td>";
    echo "</tr>";
}
?>

1 个答案:

答案 0 :(得分:0)

好的,所以你说这是你的问题。

$query = "SELECT title, count(title) as totalnotary , notary_date, book_no 
              FROM tbl_notary 
              Where 1 //!!!!!What is this 1???? You are missing a column name probably
              and notary_date between '" . $fromDate . "' and '" . $endDate . "'";

尝试此查询。您有WHERE 1无法执行任何操作,很可能使查询失败。尝试进行一些错误报告,您可能会在此处收到错误消息。

$query = "SELECT title, count(title) as totalnotary , notary_date, book_no 
              FROM tbl_notary 
              WHERE notary_date BETWEEN '" . $fromDate . "' AND '" . $endDate . "'";

然后将其放在下面以查看查询是否失败。

$Records = mysqli_query($conn, $query) or die (mysqli_error($conn));