Creating a Search feature with PHP and SQL

Associate
Joined
6 Mar 2009
Posts
495
HI guys

I have created a database in PhpMyAdmin and would like to create a search feature in my website for users to search for certain things throughout all the tables in the database.

Would like to be able to search by name, date etc.

I have done some research online and found code which i tried to use but couldn't get it working.

Code:
<? 
 //This is only displayed if they have submitted the form 
 if ($searching =="yes") 
 { 
 echo "<h2>Results</h2><p>"; 
 
 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 
 
 // Otherwise we connect to our Database 
 mysql_connect("******", "*****", "*******") or die(mysql_error()); 
 mysql_select_db("******") or die(mysql_error()); 
 
 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 
 
 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM ******** WHERE upper($field) LIKE'%$find%'"); 
 
 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 echo $result['Product']; 
 echo " "; 
 echo $result['Date']; 
 echo "<br>"; 
 echo $result['PassFail']; 
 echo "<br>"; 
 echo "<br>"; 
 } 
 
 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 
 
 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?>

When setting up my search form i have set the action to <?=$PHP_SELF?>.

When i run the search i just get a blank page.

Any suggestions folks or even a better method to use.

Thanks
 
The field value is defined in here in think!
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="Product">Product</option>
<Option VALUE="Date">Date</option>
<Option VALUE="PassFail">PassFail</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
I take it by what you guys are saying is that this is the wrong way to go about this?

Ok Ed, i defined my variables the way you have said but no joy:( Tho now im getting page cannot be displayed!!
 
Yea i put them at the top of the page. I also changed <?=$PHP_SELF?> to the filename of the php script. Now when i run it, it just prints out the php code onto the screen:(

But at least we are getting somewhere:)
 
I found the problem and feel like an idiot now!! I copied the code of the internet and the PHP tags were not properly opened! It was like this <?....?> instead of <?php...?>

Just as you talked about the tags i realized, don't know how i didn't spot that before.

Thanks again for the help:)
 
Ok now that that is sorted, another question lol

The search query that i done only searches from one table in the database. So how can i search from all tables in the database?
 
Guys thanks for the info.

Filtering is something i never came across before and will now do some research on. Done some PHP at uni but never learned about filtering.
 
Found a few good articles on filtering and now have a better understanding:)

One thing im not sure on. Is it not just data that is being inputed into a database that needs to be cleaned and filtered or is it also data that is being search from databases??
 
Yea sort of thought it would have to be filtered both ways.

Within the code at the top of the thread there is a little filtering:
Code:
$find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find);

This seems to work aswel, but will take onboard what you also said.

Will work on this for the next day or so and see how i get on.

Thanks again for the info, it has been very helpful:)
 
Last edited:
When you talk about stored procedures is that like creating classes in Java, C++ etc.

Where you have all the code in the class and then call it when you need it??
 
Done a good bit of research this morning and now im getting confused. There is so many different ways of doing things that each person suggests. So not sure how to go about it now:( lol
 
Ed, thanks again your full of wisdom and helpful information:)

I am picking things up slowly through trial and error but i will get there in the end.

Cheers
 
Back
Top Bottom