Javascript Ajax搜索未连接到数据库
我正在尝试进行实时ajax搜索。当你输入一个单词时,它会自动显示建议,就像W3学校一样。但由于某种原因,我的索引文件和php没有交换数据值,或者我的数据库由于某种原因没有连接。我总是得到“找不到国家”。你能检查代码有无错误吗? 这是php文件:Javascript Ajax搜索未连接到数据库,javascript,php,ajax,search,livesearch,Javascript,Php,Ajax,Search,Livesearch,我正在尝试进行实时ajax搜索。当你输入一个单词时,它会自动显示建议,就像W3学校一样。但由于某种原因,我的索引文件和php没有交换数据值,或者我的数据库由于某种原因没有连接。我总是得到“找不到国家”。你能检查代码有无错误吗? 这是php文件: <?php include_once('dbconnect.php'); $q = intval($_GET['q']); $query = mysqli_query($conn,"SELECT * FROM `users` WHERE u
<?php
include_once('dbconnect.php');
$q = intval($_GET['q']);
$query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'");
$count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
if($count == "0" || $q == ""){
$s[] = "No Country found!";
}else{
while($row = mysqli_fetch_array($query)){
$s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
}
}
for($x = 0; $x < $count; $x++) {
echo $s[$x];
echo "<br>";
}
?>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
function showCountry(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","indexsearchquery.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<input id="search-box" name="q" type="text" autocomplete="off" placeholder="Search country..." onchange="showCountry(this.value)" />
<input type='image' name='search' id="search-icon" value='Submit' src="search-icon.png" >
<p style="color:white;">Suggestions: <span id="txtHint" ></span></p>
您的国家/地区名称将不会是整数。但是您可以将其转换为intval
将php文件更改为
include_once('dbconnect.php');
$q = $_GET['q']; //<-----remove intval
$query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'");
$count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
if($count == "0" || $q == ""){
$s[] = "No Country found!";
}else{
while($row = mysqli_fetch_array($query)){
$s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
}
}
for($x = 0; $x < $count; $x++) {
echo $s[$x];
echo "<br>";
}
include_once('dbconnect.php');
$q=$_GET['q']// 了解dbconnect.php文件的内容很重要,因为它是包含数据库连接变量的文件。
检查您的MySQL服务器是否已启动,并且所有变量都已正确设置。调试时,发送到服务器的值是多少?运行时执行的实际SQL查询是什么?users
中的数据是什么?您希望返回什么记录?可能值得注意的是,这个答案造成了明显的SQL注入漏洞……这起到了作用。现在,我如何保护自己免受SQL注入的影响?使用准备好的语句,您将找到大量教程it@Peslis: