我目前正在为我的学校建立一个图书馆网站,但在使页面上的搜索栏返回多个结果时遇到问题。这是代码:

PHP:

  <?php
    error_reporting(E_ALL);
    $cont = mysqli_connect('host','username','password', 'db') or die(mysqli_connect_error());
    $output='';
    if(isset($_POST['searchVal'])){
      $searchkey= $_POST['searchVal'];
      $searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);

    $query = mysqli_query($cont, "SELECT * FROM books WHERE Title LIKE
     '%$searchkey%' OR Author LIKE '%$searchkey%' OR ISBN_Other_code_no LIKE
     '%$searchkey%' ") or die('couldnotconnnect');
    $count = mysqli_num_rows($query);

    if($count == 0){
        $output="There was no search result!";
    }else{
        while($row=mysqli_fetch_array($query)){
              $fname = $row['Title'];
              $lname = $row['Author'];
              $pname = $row['ISBN_Other_code_no'];

            $output = "$fname  $lname  $pname </br>";

        };
    };
    };

    echo ($output);
    ?>


JS:

function searchq(){
        var searchTxt = $("input[name='search']").val();

        $.post("search.php", {searchVal: searchTxt}, function(output){
            $("#output").html(output);
        });
};


上面的代码在应有10个或更多的情况下仅在我的HTML中发布一个结果。谁能看到代码中可能存在的错误,阻止其循环通过SQL表中的所有语句?还是我必须在PHP中编写一个循环?

谢谢你的帮助!

最佳答案

顺带一提,您会发现您正在覆盖$output而不是附加到它。尝试:

$output .= "$fname  $lname  $pname </br>";


注意:当我在手机上缓慢而笨拙地打字时,Qirel也发布了相同的建议。随时发布它作为答案。

10-05 20:28
查看更多