我编写了这段代码来启用对我的数据库的搜索,然后显示结果。(代码复制如下)
我需要一些帮助,因为我需要知道如何从搜索结果中创建链接,以便他们可以单击结果并自动在新网页上打开详细信息。
我对 PHP 非常陌生,因此任何人都可以提供任何帮助,我们将不胜感激。
谢谢
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
if($_POST['filter1'] == "Whole Site"){
$sqlCommand = "(SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%') UNION (SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%')";
} else if($_POST['filter1'] == "cars"){
$sqlCommand = "SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
} else if($_POST['filter1'] == "motorcycles"){
$sqlCommand = "SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
}
include_once("testconn2.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query)){
$id = $row["id"];
$title = $row["title"];
$search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Database </h2> <h4><a href="Sign in.php">Sign in</a> <a href="UserRegistration.php">Register</a></h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
<input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="Whole Site">Whole Site</option>
<option value="Cars">Cars</option>
<option value="Motorcycles">Motorcycles</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>