我的实时搜索栏以以下方式显示搜索结果:
[{“ item_name”:“ johnnie-walker-odyssey”,“ href”:“ profile.php?item_name = johnnie-walker-odyssey”},{“ item_name”:“ jack daniels tennessee-fire”,“ href“:” profile.php?item_name = jack daniels tennessee-fire“}。
我打算将结果显示为链接,以便当用户单击所需结果时,可以将其重定向到另一个显示结果详细信息的页面。
<?php
$link = mysqli_connect("localhost", "root", "charles", "shoppingcartdemo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$term = mysqli_real_escape_string($link, $_REQUEST['term']);
if(isset($term)){
// Attempt select query execution
$sql = "SELECT * FROM shopping_items WHERE item_name LIKE '" . $term . "%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
//display search results links
$array [] = array("item_name"=> $row['item_name'],"href"=>"profile.php?
item_name=".$row['item_name']);}
echo json_encode($array)
// Close result set
mysqli_free_result($result);
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
?>