Hey.. I'm making a search function for my forum... Now, first off, heres the code:
$wordsearch = $_POST['searchwords'];
$usersearch = $_POST['searchuser'];
$sectionsearch = $_POST['searchsection'];
if ($usersearch!="") {
if (mysql_num_rows(mysql_query("SELECT * FROM Forum_Users WHERE username='$usersearch'",$connection))==0) {
echo "The username <i>$usersearch</i> does not exist. Please try again<br><br><a href='ForumSearch.php'>Return</a>";
unset($usersearch,$wordsearch,$sectionsearch,$amountresults);
exit();
}
$usersqlbit = "AND reply_poster='$usersearch'";
}else{
$usersearch=="%";
$usersqlbit = "";
}
$explodedwords = explode(" ", $wordsearch);
$wordcount = count($explodedwords);
if ($wordcount>1) {
for ($i=1; $i<$wordcount; $i++) {
$sermid .= " OR reply_message LIKE '%".$explodedwords[$i]."%'";
}
$serstart = "reply_message LIKE '%".$explodedwords[0]."%'";
$serwhole = $serstart.$sermid;
}else{
$serwhole = "reply_message LIKE '%".$wordsearch."%'";
}
$sqlwhole = "SELECT thread_id FROM Forum_Replies WHERE $serwhole $usersqlbit ORDER BY reply_postdate DESC, reply_posttime DESC";
$dosqlwhole = mysql_query($sqlwhole,$connection) or die(mysql_error());
if (mysql_num_rows($dosqlwhole)==0) {
echo "Your search returned no results<br><br><a href='ForumSearch.php'>Return</a>";
exit();
}
while ($getthid=mysql_fetch_array($dosqlwhole)) {
$serth = $getthid['thread_id'];
if ($allthr=="") {
$allthr = $serth;
}else{
$allthr = $allthr.",".$serth;
}
}
if ($sectionsearch=="%") {
$secsql = "";
}else{
$secsql = "section_id= $sectionsearch AND";
}
$threadsexploded1 = explode(",", $allthr);
$threadsexploded = array_unique($threadsexploded1);
$threadcount = count($threadsexploded);
if ($threadcount>1) {
for ($r=1; $r<$threadcount; $i++) {
$smid .= " OR thread_id='$threadsexploded[$r]'";
}
$sstart = "thread_id='$threadsexploded[0]'";
$sfinal = $sstart.$smid;
}else{
$sfinal = "thread_id='$allthr'";
}
$thsql = mysql_query("SELECT * FROM Forum_Threads WHERE $secsql $sfinal ORDER BY thread_lastpostdate DESC, thread_lastposttime DESC", $connection);
if (mysql_num_rows($thsql)==0) {
echo "Your search returned no results<br><br><a href='ForumSearch.php'>Return</a>";
exit();
}
while ($gett=mysql_fetch_array($thsql)) {
$tsub = $gett['thread_subject'];
$tsta = $gett['thread_starter'];
$tsta = "<a href='ForumViewUser.php?username=$tsta'>$tsta</a>";
$tlpo = $gett['thread_lastposter'];
$tlpo = "<a href='ForumViewUser.php?username=$tlpo'>$tlpo</a>";
$tlpd = $gett['thread_lastpostdate'];
$tlpt = $gett['thread_lastposttime'];
$tpc = $gett['thread_postcount'];
$tvc = $gett['thread_viewcount'];
$tsed = $gett['section_id'];
$getsid = mysql_result(mysql_query("SELECT section_name FROM Forum_Sections WHERE section_id='$tsed'",$connection),0);
$showsearch .= "<tr><td valign='center'>$tsub</td><td align='center' valign='center'>$getsid</td><td align='center' valign='center'>$tsta</td><td>Posted by: <i>$tlpo</i><br>at $tlpd $tlpt</td><td align='center' valign='center'>$tpc</td><td align='center' valign='center'>$tvc</td></tr>";
}
echo "<table width='95%' border='1' cellspacing='1' cellpadding='5' align='center'><tr><td width='35%'><b><i>Thread Title</i></b></td><td align='center'><b><i>Section</i></b></td><td align='center' width='22%'><b><i>Thread Starter</i></b></td><td align='center'><b><i>Last Post Info</i></b></td><td align='center' width='7%'><b><i>Posts</i></b></td><td align='center' width='7%'><b><i>Views</i></b></td></tr>";
echo $showsearch;
echo "</table>";
unset ($allthr);
?>
I know it might look a bit confusing but basically... if the submitted word(s) appear in more than one thread i get a 'The Page cannot be displayed' error. If the word(s) appear in one thread... its fine... i just cant figure out why i get the error.
Would someone please mind having a look and telling me if they can see something wrong.. I've been struggling with it for hours 🙁
Many Thanks, BIOSTALL