简体   繁体   中英

How to show records between Dates in search?

It only shows 'No record found' but I'm trying to show the records between the dates using search

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

OK, so you are saying that here is your problem.

$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 . "'";

Try this query. You have WHERE 1 which can't do anything and probably is failing your query. Try with some error reporting and you will probably get an error there.

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

Then put this below to see if query is failing or not.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM