Page 1 of 1

How to create a simple address search?

Posted: Wed Mar 09, 2011 4:46 pm
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.

Re: How to create a simple address search?

Posted: Wed Mar 09, 2011 6:28 pm
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>"; 
}

}

Re: How to create a simple address search?

Posted: Thu Mar 10, 2011 4:10 pm
by GradyLorenzo
The browser complained about that bit..

Re: How to create a simple address search?

Posted: Thu Mar 10, 2011 10:19 pm
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'])){