Jump to content

[SOLVED] Searching Using PHP


crochk

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.