在我的数据库中运行搜索后,结果以链接的形式显示,以便他们将我重定向到特定页面...例如,当我搜索类别业务时,它显示该类别但是当我点击它时它将我重定向到另一个类别的内容..当我检查我的网址时,我注意到它像
http://mysite/forum%20part%20two/view_category.php?cid=1
而不是
http://mysite/forum%20part%20two/view_category.php?cid=2
这是我的搜索代码
if(isset($_POST['search'])){ //form submitted, clicked Submit Search
$query = strip_tags(mysql_real_escape_string($_POST['query'])); //try to prevent sql injections
if(!$query){ //not enterered a query
echo 'You must enter a search query!';
}else{
//EDIT THIS ----------------------------------
$table = 'categories'; //the table you want to search
$row = 'category_title'; //the row in which you want to search
//EDIT THIS ----------------------------------
$sql = mysql_query("SELECT * FROM `".$table."` WHERE `".$row."` LIKE '%".$query."%'"); //search query
if($sql){ //no errors
if(mysql_num_rows($sql) == 0){ //No results found.
echo 'No results were found for <strong>'.$query.'</strong>';
}else{ //one or more results have been found
echo 'We have found <strong>'.mysql_num_rows($sql).'</strong> for <strong>'.$query.'</strong>.<br><br>
<table>
<tbody>
<tr>
<td><strong>category_title</strong></td>
</tr>';
while($r = mysql_fetch_array($sql)){ //get data of every user where their category_title is like the $query string
$category_title = $r["category_title"];
//lets put the part they searched in bold.
$category_title = str_ireplace($query, '<strong>'.$query.'</strong>', $category_title);
//lets put the part they searched in bold.
echo '<td>'.$category_title."<a href='view_category.php?cid=".$id."' class='cat_links'>".$category_title." - <font size='-1'>".$description."</font></a></td>
</tr>'";
}
echo '</tbody></table>';
}
}else{
echo 'Sorry, an MySQL error occurred:<br><br>'.mysql_error(); //an error occurred, so echo it
}
}
}
我的view_category.php代码就是这个
<?php
// Connect to the database
include_once("connect.php");
// Function that will count how many replies each topic has
function topic_replies($cid, $tid) {
$sql = "SELECT count(*) AS topic_replies FROM posts WHERE category_id='".$cid."' AND topic_id='".$tid."'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
return $row['topic_replies'] - 1;
}
// Function that will convert a user id into their username
function getusername($uid) {
$sql = "SELECT username FROM users WHERE id='".$uid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
return $row['username'];
}
// Function that will convert the datetime string from the database into a user-friendly format
function convertdate($date) {
$date = strtotime($date);
return date("M j, Y g:ia", $date);
}
// Assign local variables
$cid = $_GET['cid'];
// Check to see if the person accessing this page is logged in
if (isset($_SESSION['username'])) {
$logged = " | <a href='create_topic.php?cid=".$cid."'>Click Here To Create A Topic</a>";
} else {
$logged = " | Please log in to create topics in this forum.";
}
// Query that checks to see if the category specified in the $cid variable actually exists in the database
$sql = "SELECT id FROM categories WHERE id='".$cid."' LIMIT 1";
// Execute the SELECT query
$res = mysql_query($sql) or die(mysql_error());
// Check if the category exists
if (mysql_num_rows($res) == 1) {
// Select the topics that are associated with this category id and order by the topic_reply_date
$sql2 = "SELECT * FROM topics WHERE category_id='".$cid."' ORDER BY topic_reply_date DESC";
// Execute the SELECT query
$res2 = mysql_query($sql2) or die(mysql_error());
// Check to see if there are topics in the category
if (mysql_num_rows($res2) > 0) {
// Appending table data to the $topics variable for output on the page
$topics = "<table width='100%' style='border-collapse: collapse;'>";
$topics .= "<tr><td colspan='4'><a href='index.php'>Return To Forum Index</a>".$logged."<hr /></td></tr>";
$topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Last User</td><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
$topic = "<tr><td colspan='4'><hr /></td></tr>";
// Fetching topic data from the database
while ($row = mysql_fetch_assoc($res2)) {
// Assign local variables from the database data
$tid = $row['id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
// Check to see if the topic has every been replied to
if ($row['topic_last_user'] == "") { $last_user = "N/A"; } else { $last_user = getusername($row['topic_last_user']); }
// Append the actual topic data to the $topics variable
$topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".getusername($creator)." on ".convertdate($date)."</span></td><td align='center'>".$last_user."</td><td align='center'>".topic_replies($cid, $tid)."</td><td align='center'>".$views."</td></tr>";
$topics .= "<tr><td colspan='4'><hr /></td></tr>";
}
$topics .= "</table>";
// Displaying the $topics variable on the page
echo $topics;
} else {
// If there are no topics
echo "<a href='index.php'>Return To Forum Index</a><hr />";
echo "<p>There are no topics in this category yet.".$logged."</p>";
}
} else {
// If the category does not exist
echo "<a href='index.php'>Return To Forum Index</a><hr />";
echo "<p>You are trying to view a category that does not exist yet.";
}
?>
抱歉粘贴了很多代码
答案 0 :(得分:1)
echo '<td>'.$category_title."<a href='view_category.php?cid=".$id."' class='cat_links'>".$category_title." - <font size='-1'>".$description."</font></a></td>
</tr>'";
不能从任何地方获取变量$ id,这可能就是原因。请从此
附近的查询结果中获取其值 $category_title = $r["category_title"];
可能是id
$id = $r["id"];