無法找出這裏出了什麼問題,沒有錯誤出現,但沒有任何被返回。搜索欄應該從我的數據庫中返回其他用戶的名字。搜索欄不會返回結果
這裏是test.php的:
<?php
session_start();
?>
<!DOCTYPE html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function search(partialSearch){
$.ajax({
url:"PHP.php",
type:"POST",
data {partialSearch:partialSearch},
success:function(result){
$("#results").html(result);
}
});
};
</script>
</head>
<body>
<div class="container">
<h3>Find Other users</h3>
<input type="text" name="partialSearch"onkeyup="search(this.value)"/>
<div id="results"></div>
</div>
</body>
</html>
這裏是PHP.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "coursework_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$partialSearch = $_POST['partialSearch'];
$stmt = $conn->prepare("SELECT username FROM members WHERE username LIKE ? ");
$stmt->bind_param('s',$partialSearch);
$stmt->execute();
$stmt->bind_result($username);
while ($row = $stmt->fetch()) {
$searchResults[] = $username;
echo "<div>".$searchResults."</div>";
}
?>
下面是SQL表:
CREATE TABLE members (
memberID int(5) NOT NULL AUTO_INCREMENT,
username VARCHAR(65) NOT NULL UNIQUE,
password VARCHAR(65) NOT NULL,
PRIMARY KEY (memberID)
);
預先感謝您的幫助!
您是否嘗試過檢查控制檯日誌中的錯誤? –
「沒有錯誤出現」,因爲你不檢查錯誤。您在js和php中都有錯誤。在JS中,你至少有一個括號問題。在PHP你的結果是'
嗯,我得到了這個工作。你應該能夠把這一切都拼湊起來。 –