简体   繁体   English

即时搜索仅返回一项

[英]Instant Search only returns one item

I am currently building a library website for my school and am having a problem getting the searchbar on the page to return more than one result. 我目前正在为我的学校建立一个图书馆网站,但在使页面上的搜索栏返回多个结果时遇到问题。 Here is the code: 这是代码:

PHP: 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: JS:

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

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

The code above only posts one result in my HTML when there should be 10 or more. 上面的代码在应有10个或更多的情况下仅在我的HTML中发布一个结果。 Can anyone see a possible bug in the code stopping it from looping through all of the statements in the SQL table? 谁能在代码中看到可能的错误,阻止它循环遍历SQL表中的所有语句? Or do I have to write a loop in the PHP? 还是我必须在PHP中编写一个循环?

Thanks for your help! 谢谢你的帮助!

Putting other issues to the side, it stands out you are overwriting $output instead of appending to it. 抛开其他问题,可以看出您正在覆盖$output而不是附加到$output Try: 尝试:

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

Note: Qirel posted this same advice while I was slowly and awkwardly typing on my mobile. 注意:当我在手机上缓慢而笨拙地打字时,Qirel发表了同样的建议。 Feel free to post it as an answer. 随时发布它作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM