首页 > 解决方案 > php没有返回任何回声

问题描述

我有一个表格,它显示来自 mysql 数据库的图书列表和用户可以搜索图书的搜索表单。当用户通过标题和作者的输入值进行搜索时,我期待按标题和作者显示图书列表,并在没有任何记录时显示“NO Books by name or author”作为回显。

我的代码是

<!DOCTYPE HTML>
<html>
    <body bgcolor="87ceeb">
        <center><h2>Central Department of Physics</h2></center>
        <br>
        <?php
        include("DBConnection.php");
        $search = isset($_REQUEST["search"]) ?  $_REQUEST["search"] : '';

        $query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'"; 
        //search with a book name in the table book_info
        $result = mysqli_query($db,$query);


        ?>
        <a href="searchform.php" class="btn btn-primary">Go Back</a>
        <table border="2" align="center" cellpadding="5" cellspacing="5">
            <tr>
                <th> ISBN </th>
                <th> Title </th>
                <th> Author </th>
                <th> Edition </th>
                <th> Publication </th>
            </tr>
            <?php 
            if(mysqli_num_rows($result)>0){
                while($row = mysqli_fetch_assoc($result))
                {
            ?>
                <tr>

                    <td><?php echo $row["ISBN"];?> </td>
                    <td><?php echo $row["Title"];?> </td>
                    <td><?php echo $row["Author"];?> </td>
                    <td><?php echo $row["Edition"];?> </td>
                    <td><a href="<?php  echo $row["Publication"];?>" target="_blank">Click Me</a></td>

                </tr>

            <?php

                }

            }
            else{ ?>
                <tr>
                    <td colspan="5">
                        <center>No books found in the library by the name $search </center>
                    </td>
                </tr>

        <?php } ?>
        </table>
        
    </body>
</html>
<br>

我的搜索表单是

<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">

<form action = "DisplayBooks.php" method="get">
<br>
<center>Enter the title of the book to be searched :
<input type="text" name="search" size="48">
<br></br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</center>
<br>
</form>
</body>
</html>

但是它成功地显示了书籍列表但是当没有任何记录时..它不会启动回声。

附言。如何添加链接按钮,使其显示返回搜索结果以将用户导航到 Searchform,并且用户可以返回上一个表单。

标签: phpmysqlformsmysqli

解决方案


您已重复检查“无结果”:

if(mysqli_num_rows($result)>0)if(mysqli_num_rows($result)>0)

删除一个。


推荐阅读