简体   繁体   中英

Missing the result printed from database

My purpose is search book type from input keyboard and count it. I have error: counting the number of book is right but when print, it miss some result. For example, I put 'f' it show 7 result( right) however it print only 2 two result. Here is my code:

<form action="" method="POST">
        Enter type of book here:<input type="text" size="20" name="sbt"> <br />
        <input type="submit" name="sb" value="Search">
    </form>
    <table align="center" border="1" width="600">
        <thead><tr align="center">
    <tr align="center">
            <td><b>Book ID</b></td>
            <td><b>Book Title</b></td>
            <td><b>Book Author</b></td>
            <td><b>Pulished Year</b></td>
            <td><b>Book Type</b></td>
            <td><b>Status</b></td>
        </tr>
        <?php
        if (isset($_POST['sb'])) {
            $s="";
            if ($_POST['sbt'] == null) {
                  echo "Please re-enter <br>";
                } else
                {
                  $s = $_POST['sbt'];
                }
            $q = "SELECT * FROM book WHERE book_type LIKE '%$s%' ";
            $r= mysqli_query($conn,$q);
            while($row = mysqli_fetch_array($r)) {
             ?>
        <tr align="center">
                <td><?php echo $row['book_no'];?></td>
                <td><?php echo $row['book_title'];?></td>
                <td><?php echo $row['book_author'];?></td>
                <td><?php echo $row['book_year'];?></td>
                <td><?php echo $row['book_type'];?></td>
                <td>
                <?php 
                if ($row['book_quantity'] == 1) {
                                echo "Available";
                            }
                else {
                                echo "Not available"; 
                            }  

                ?>
                 </td>

            <?php
             $c=" SELECT COUNT(DISTINCT(book_no)) AS totaltype FROM book WHERE book_type LIKE '%$s%'";
             $s= mysqli_query($conn, $c);
             if (mysqli_num_rows($s)> 0 )
             {
                $row2= mysqli_fetch_array($s);
                echo " Total book type result:"."{$row2['totaltype']}";
            }
        } 

        ?>
         <?php } ?>
    </table>

Remove "%" before $s and try.

for example:

$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '$yourPHPVAR%'");

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