【问题标题】:php not returning any echophp没有返回任何回声
【发布时间】:2019-04-24 16:47:21
【问题描述】:

我有一个表格,它显示来自 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>

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

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

【问题讨论】:

  • 这对 SQL 注入开放。如果您重视为此付出的时间和精力,请使用准备好的陈述。我们不希望您的数据库遭到破坏并可能被删除。
  • @FunkFortyNiner 不,这只是测试,没关系....由于 Delwar,回声问题已得到解决,但我想添加按钮以从结果返回 Serchform....只有我没有得到。

标签: php mysql forms mysqli


【解决方案1】:

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

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

删除一个。

【讨论】:

  • 这个答案很清楚,恕我直言,应该是公认的答案。另一个是“试试这个”,我很难比较他们到底做了什么才值得被接受/授予。据我所知,OP 的代码可能有更多问题。
  • “这么神秘的问题肯定有更复杂的解决方案”——非专家
【解决方案2】:

试试这个:

<!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);


        ?>
        <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>

【讨论】:

  • 好的,它可以工作..最后一个问题如何添加链接,以便它显示书籍结果或回显..会有一个带有返回搜索的按钮,当用户点击它时,它将导航回来搜索表格。有什么帮助吗?
  • 您可以在显示结果页面的任何位置添加锚标记.... 像这样 -> 返回 ...btn btn-primary -> 基本上这些类是引导程序中按钮的设计
  • 嗯,但我必须添加 displayBooks.php 表单,即使没有进行搜索,它也会显示....我只想在用户转到 serchform 并单击提交按钮时显示它它将仅打开与结果匹配的书籍的 Displaybooks 表单,并显示链接以再次返回到搜索表单,类似于仅在 displaybooks 表单中进行搜索时显示的 echo ...!!
  • 返回....把它添加到桌面上的 displayBooks.php 中。
  • 请通过编辑您的答案告诉大家,告诉我们您究竟做了什么来解决这个问题。很难在 OP 的代码和此处的答案之间进行比较。