我写了这个代码以使搜索我的数据库,并以然后显示结果。 (下面的代码复制)如何添加一个链接到搜索结果
我需要一些帮助,因为我需要知道如何创建,然后从搜索结果中的链接,使他们能够在结果上点击,它会自动打开一个新的网页的细节。
我很新的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>
这不是他所问的。他的搜索结果工作正常。他无法弄清楚如何使用单个项目生成页面。 – 2012-08-02 22:55:19
@ B-RiZzY:好的,这就是我从这个问题所理解的。 – Jocelyn 2012-08-02 22:56:21
感谢您的回复,但我不认为我对我的问题非常清楚......对不起!我基本上需要创建一些代码,当选择项目(链接)时,现有数据将发送到新页面,而不是再次查询数据库。 这是可能的,有没有关于这种方法的任何想法,而不是创建一个单独的页面?这是否会减少服务器活动并且它有多安全? 谢谢 – user1572621 2012-09-05 21:11:40