Php 选择2&x27;不能在服务器上工作
在localhost(XAMPP)服务器上使用Select2从数据库获取数据时,它工作得非常好。但在将其部署到服务器后,它无法工作,并且控制台显示500个内部服务器错误 以下是我正在使用的代码:Php 选择2&x27;不能在服务器上工作,php,ajax,jquery-select2,Php,Ajax,Jquery Select2,在localhost(XAMPP)服务器上使用Select2从数据库获取数据时,它工作得非常好。但在将其部署到服务器后,它无法工作,并且控制台显示500个内部服务器错误 以下是我正在使用的代码: $('#search-box').select2({ placeholder: 'Select an item', ajax: { url: 'php/search.php', dataType: 'json', delay: 250,
$('#search-box').select2({
placeholder: 'Select an item',
ajax: {
url: 'php/search.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
以及search.PHP中的PHP代码:
require_once("dbinfo.php");
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) { die('Not connected : ' . mysqli_error($connection));}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$sql = "SELECT c_id, city_name FROM city
WHERE city_name LIKE '%".$_GET['q']."%'
LIMIT 10";
$result = $connection->query($sql);
$json = [];
while($row = $result->fetch_assoc()){
$json[] = ['id'=>$row['c_id'], 'text'=>$row['city_name']];
}
echo json_encode($json);
这里出了什么问题?在ajax中,将type设置为“GET”
这对我来说很有用。500错误表示您的PHP有问题。有关详细信息,请查看web服务器的错误日志。我没有看到任何数据被发送到服务器,在AJAX调用中的URL或数据属性上没有查询字符串(
?q=search\u term
)(数据:{q:“search\u term”}
),因此可能出现500错误,因为您没有定义$\u GET[q']
。表示了解的语句。即使是这样也不安全!但所有这些事情都发生在本地服务器上。我一定会查看错误日志。我还将研究准备好的语句,包括jQuery库。除了我提到的那个错误外,没有其他错误。我正在使用Apache服务器。
ajax: {
url: 'php/search.php',
type: 'GET',
dataTy.......