Jump to content

Allowing the user to search your database?


mookid

Recommended Posts

Hi there,

 

I am fairly new to the php/Mysql world and I had a few questions that i am having a hard time finding answers for (i've tried various "google" searches which leads to tutorials with code that usually doesn't work).

 

I am currently looking to build a website with the ability to allow users to search for certain types of stores in their area. I have a database created with phpMyAdmin that i've populated with all the necessary information (name, city, zip, etc etc). I have been desperately looking for a fairly simple way to just type in a zip code (in an html search form) and have it spit back results to the user based on the zip code they entered.

 

I don't expect any truly easy answers or even a full tutorial/walktrhough, just something that will point me in the right direction (books, websites, pre-written tutorials) that will allow me to do something like this.

 

This seems like something that is VERY common on the internet, and the lack of information i've been able to find on this is really surprising. Thanks for ANY and ALL help!

I have a search on my site and it's fairly simple.  the easiest way I found to do it is Take the post from your form say $_POST['zip'] or whatever the name of your input area where the user enters the zipcode.  Then have the form submit to it's own page using $_SERVER['PHP_SELF'] and then have a section that looks for the form being posted.  ie.  If the button name is Submit then have it look for $_POST['submit']. 

Turn $_POST['zip'] into $zip for easier use in mysql

$zip = $_POST['zip'] then do a mysql query that will say SELECT * FROM  table_name WHERE field_name = '$zip'";

 

This will search your database for all records that contain that zip code. The same premise would be used for any search query.

how about splitting the inputed zipcode into the first 3 or 4 numbers (eg 92707 becomes 927 or 9270) and you use query the database for all zipcodes, then maybe use strstr(); to find the 3/4 4 digit zip, then echo anything that matches...and within those results try matching up the full zip code, echoing the code that matches the whole first...

I have written a tutorial on searching your database it goes through each part a sections at a time then provides the full script at the end you can also add comments if you have any questions.

 

Might be helpful.

 

http://www.newmedia.lincoln.ac.uk/dcarr/tutorials/php/searching-with-php-and-mysql-beyond-like

 

you can set it to search in one or more columns so you could set it to search your zip column.

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.