根据我收到的错误消息,我的 SQL 语法有问题,但无法弄清楚。我正在尝试创建一个系统,我可以在其中使用 AJAX、PHP 和 SQL 搜索项目名称以查找匹配项。与搜索词的每个匹配项都以超链接的形式显示在屏幕上,单击该超链接后,我打算将我带到另一个页面,然后该页面将显示与搜索词相关的任何内容。
我已将其设置为搜索数据库并显示超链接,但是当我单击超链接以加载相关匹配项时,我收到错误消息“您的 SQL 语法有错误;请查看与您的 MySQL 相对应的手册服务器版本,以便在第 1 行的 'Home From Home''' 附近使用正确的语法
现在在代码中,您不会在任何地方找到 Home From Home 引用,因为那是“search_value”选择的搜索词。我认为我的问题与我的查询有关,但我不是 100% 确定。我将发布下面所有 3 个页面的代码,以便您查看所有页面如何链接在一起等。
我哪里错了?
CPanel.php
<html>
<head>
<script>
function showHint(str) {
if (str.length==0) {
document.getElementById("results_box").innerHTML="Matching Project Names: <hr/>";
return;
}
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 (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("results_box").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","CPanel_Inc.php?search_value="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p><b>Project Search:</b></p>
<form>
Project Name: <input type="text" onKeyUp="showHint(this.value)" size="20" />
</form>
<div id="results_box">
Matching Project Names: <hr/>
</div>
</body>
</html>
CPanel_Inc.php
<?php
$connect = mysqli_connect("localhost", "root", "", "creative wolf");
if (mysqli_connect_errno()) {
printf("Connect Failed", mysqli_connect_error());
exit();
} else {
// Get the search_value parameter from URL
$search_value = $_GET["search_value"];
$content = "";
// Search database for comparable values provided that search_value has some value
if (strlen($search_value) > 0) {
// Sets up query return results
$qry = "SELECT ProjectName FROM projects WHERE ProjectName LIKE '".mysql_real_escape_string($search_value)."%'";
$res = mysqli_query($connect, $qry) or die(mysqli_error($connect));
if (mysqli_num_rows($res) < 1){
printf("Matching Project Names: <hr/> Could not retrieve records matching your search critera", mysqli_error($connect));
} else {
$content .= "Matching Project Names: <hr/>";
while ($data = mysqli_fetch_array($res)) {
$content .= "<li><a href=\"search.php?search_value='". $data['ProjectName']."'\"> " . $data['ProjectName']. "</a></li>";
}
echo "$content";
}
}
mysqli_free_result($res);
mysqli_close($connect);
}
?>
搜索.php
<?php
$connect = mysqli_connect("localhost", "root", "", "creative wolf");
if (mysqli_connect_errno()) {
printf("Connect Failed", mysqli_connect_error());
exit();
} else {
// Get the search_value parameter from URL
$content = "";
// Set up query
$get_data_qry = "SELECT * FROM content WHERE ProjectName = '".$_GET["search_value"]."'";
$get_data_res = mysqli_query($connect, $get_data_qry) or die(mysqli_error($connect));
while ($clientData = mysqli_fetch_array($get_data_res)) {
$content .= $clientdata['ProjectName'];
$content .= $clientdata['RequestedBy'];
$content .= $clientdata['FileLocation'];
$content .= $clientdata['Description'];
$content .= "<br />";
}
mysqli_free_result($res);
mysqli_close($connect);
}
?>