Jump to content

Recommended Posts

I have a MySQL database of files.  I want the users to be able to search the database by the content using FULLTEXT searching or search by id#.  The search form is a text filed (find) and a drop down menu to choose the field (field).

 

The problem:

My search php code is having a small problem.  I know the id code and content code work seperatly, but when combined as below, only the first one will work (in this case 'content').

 

<?php
//PROCESS SEARCH
if(isset($_POST['search']) && isset($_POST['find']))
{


//GET INFORMATION FROM FORM
$search = $_POST['find'];
$field = $_POST['field'];


//SEARCH DATABASE
if($field = 'content')
{
$sql = mysql_query("SELECT * FROM submit WHERE MATCH (filetitle,filecontent) AGAINST('$search')") or die('Error: ' . mysql_error());
}

elseif($field = 'id')
{
$sql = mysql_query("SELECT * FROM submit WHERE submitID = $search");
}

echo "<h2>RESULTS</h2>";

//IF NO MATCHES	
$matches = mysql_num_rows($sql);
if($matches == '0')
	{
	exit("No matches found");


	}
else
	{
	echo "<p><strong>Number of Results: " . $matches;
	echo "</strong></p><p> </p>";



//DISPLAY RESULTS
echo "<div id='searchScroll'>";

while($row = mysql_fetch_array($sql))
	{
echo "<div id='entry'>
  <p>#" . $row['submitID'] . " - <a href= ../pages/download.php?id=" . urlencode($row['submitID']) . ">" . $row['filetitle'] . "</a><br />
    <span id=entryid>Grade " . $row['filegrade'] . " - " . $row['filesubject'] . "<br />
    Year Created: " . $row['fileyear'] . "</span></p>
</div>";
	}
echo "</div>";
}
}

?>

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/124886-solved-searching-using-php/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.