我很难将撇号传递到我的自动完成中。数据库包含不带撇号的条目...而是使用 html 等效项 (&POUND039;)。我对 ajax 还是很陌生,所以我非常感谢您的帮助。问题是,我如何传入撇号的值并使其匹配?另外,我的代码容易受到注入攻击吗?在此先感谢您的帮助!
代码是这样的:
<script type="text/javascript">
var options = {
serviceUrl:'autocomplete/autoQuery.php',
minChars:2,
delimiter: /(,|;)\s*/, // regex or character
maxHeight:400,
width:500,
zIndex: 9999,
deferRequestBy: 0, //miliseconds
params: { country:'Yes' }, //aditional parameters
noCache: false, //default is false, set to true to disable caching
// callback function:
// onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
onSelect: function(value, data){ window.location = "textbooks.php?bk=" + data;}
};
$('#query').autocomplete(options);
</script>
我有一个 php 脚本发回查询结果。该页面称为 query.php,代码如下:
$get = htmlentities($_GET['query']);
$query = "SELECT title,author,id,isbn10,isbn13 FROM textbook
WHERE title LIKE '%" . $get . "%'
OR author LIKE '" . $get . "%'
OR isbn10 LIKE '" . $get . "%'
OR isbn13 LIKE '" . $get . "%'
LIMIT 5
";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result) == 0){
$resString = "'No result found. Click here',";
$idString = "'unknown',";
}else{
$resString = "";
$idString = "";
while($data = mysql_fetch_array($result)){
$resString .= "'" . $data['title'] . " by " . $data['author'] . "',";
$idString .= "'" . $data['id'] . "',";
}
}
$resString = rtrim($resString, ',');
$idString = rtrim($idString, ',');
$code = "{\n";
$code .= "query:'" . $get . "',\n";
$code .= "suggestions:[" . $resString . "],\n";
$code .= "data:[" . $idString . "]\n";
$code .= "}";
echo $code;