How to create a simple address search?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GradyLorenzo
Forum Newbie
Posts: 6
Joined: Wed Mar 09, 2011 4:40 pm

How to create a simple address search?

Post by GradyLorenzo »

Hi all, first post here.

I have a database here containing name, zip code, and address fields. I also have a page with a field n which you would type a zip code. The idea is that if that zip code exists in the database, it would return the name, zip code, and address that are associated with that zip code. Simple, or no? A simple address look-up using only a zip code.
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: How to create a simple address search?

Post by Peec »

Hint:

Code: Select all


mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

if (isset($_POST['zipcode']){

$sql = "SELECT name,zip,address FROM table WHERE zip='".mysql_real_escape_string($_POST['zipcode'])."' ";

$qry = mysql_query($sql);

while($data = mysql_fetch_array($qry)){
   echo "<div>".$data['name']." , ".$data['zip']." , ".$data['address']."</div>"; 
}

}
GradyLorenzo
Forum Newbie
Posts: 6
Joined: Wed Mar 09, 2011 4:40 pm

Re: How to create a simple address search?

Post by GradyLorenzo »

The browser complained about that bit..
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: How to create a simple address search?

Post by jim.barrett »

did you get an error message from the code that was posted by Peec? It looks like it will work, except for one typo...

Code: Select all

if (isset($_POST['zipcode']){
you need to add another )...so

Code: Select all

if (isset($_POST['zipcode'])){
Post Reply