0
我目前正在爲我的學校建立一個圖書館網站,並且在頁面上的搜索欄返回多個結果時遇到問題。下面是代碼:即時搜索只返回一個項目
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);
});
};
上面的代碼只有一個職位結果我的HTML的時候應該有10個或更多。任何人都可以在代碼中看到一個可能的錯誤,以阻止它循環遍歷SQL表中的所有語句?或者我必須在PHP中編寫一個循環?
感謝您的幫助!
不要發佈數據庫連接細節等敏感數據。 –
我已將您的憑據編輯到數據庫中 - 我建議您立即更改密碼**,因爲它可能仍然存在安全風險! – Qirel
實際的問題是,你在每次迭代中覆蓋'$ output'。 Concat或使用數組。更改'$ output =「$ fname $ lname $ pname」;'到'$ output。=「$ fname $ lname $ pname」;'(注意從'='改爲'。=')。 – Qirel