【问题标题】:How can I tell the user if there is no data in database for particular date that input by the user我如何告诉用户数据库中是否没有用户输入的特定日期的数据
【发布时间】:2019-09-24 08:38:47
【问题描述】:

我正在做一个报告系统,它可以从数据库中收集数据并将其显示给用户。用户需要输入日期才能查看该特定日期的数据。会有某天在数据库中没有任何数据。所以我想告诉用户数据库中没有他们输入日期的记录,我应该如何处理我的代码?

这是日期搜索的代码(daily.html):

<form action="viewdailyreport.php" id="view" method="post">
<table>
<tr><th>Select Date to View Report: </th><td><input type="date" id="date" class="datepicker" name="date" value="date" required></td></tr>
<tr><td><br></td><td><br></td></tr>
<tr><td><br></td><td><br></td></tr>
<td> </td><td><input type="submit" class="button" value="View"></td>
</table>
</form>

viewdailyreport.php:

echo "<table class='report' style='background-color:white;'>";
        echo "<tr >";
        echo "<th>Date</th>";
        echo "<th>Driver id</th>";
        echo "<th>Driver name</th>";
        echo "<th>Vehicle id</th>";
        echo "<th>Vehicle model</th>";
        echo "<th>Booked services</th>";
        echo "</tr>";

        while($row=mysqli_fetch_assoc($result)){

            echo "\t<tbody><tr>
                    <td>".$row['Booking_date']."</td>
                    <td>".$row['Driver_id']."</td>
                    <td>".$row['Driver_name']."</td>
                    <td>".$row['Vehicle_id']."</td>
                    <td>".$row['Vehicle_model']."</td>
                    <td>".$row['Booking_service_type']."</td>

                    </tr></tbody>\n";

        }
    echo "</table>";

我希望当用户搜索数据库中没有任何记录的日期时,它会提示一条消息告诉用户没有记录。

【问题讨论】:

  • if(!mysqli_num_rows($result)) { echo "No entries for that date"; }
  • 我不会为您的viewdailyreport.php 中的每一行写一个echo。像在while 中那样做多行echo

标签: php mysql


【解决方案1】:

现在它不会进行迭代,因为它当然没有要迭代的项目。要在没有结果时向用户发送消息,您可以使用 mysqli_num_rows() 函数。它将返回一个integer


在你的情况下,这样的东西适合

if( ! mysqli_num_rows($result) )
{
  echo 'Couldn\'t find any rows.';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多