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
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);
});
};
The code above only posts one result in my HTML when there should be 10 or more. Can anyone see a possible bug in the code stopping it from looping through all of the statements in the SQL table? Or do I have to write a loop in the PHP?
Thanks for your help!