I'm having issues with my SQL syntax according to the error message I'm getting, but can't figure it out. I'm trying to create a system where I can search a project name for matches using AJAX, PHP and SQL. Each match to the seach term is displayed on screen as a hyperlink which when clicked I intend to take me to another page that will then display any content thats relivant to the search term.
I've got it set up to search the database and display the hyperlinks but when I click on the hyperlink to load the relivant matches I get the error message "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Home From Home''' at line 1"
Now inside the code you won't find Home From Home referanced anywhere becuase thats the search term that "search_value" picks up. I think my issue is with my query but I'm not 100% sure. I'll post the code from all 3 pages below so you can see how all the pages links together etc.
Where am I going wrong?
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);
}
?>
Search.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);
}
?>