PHP script to select state

I have a database table called contacts with a table in it called addresses. The fields in the addresses table are firstname, lastname, address, city, state, zip, areacode and phonenumber. (The state field is a 2 character text field.) I want to ask the user for a state and then I want to display all the records for that state only. Can anyone help me?

Thanks,

Andrew

i had a problem similar to this yesterday, if you look on the post prior to this one, you should be able to enter your variables and get it to work, be sure you had the WHERE clause info that Coder posted, just change variables and should work great, mine does.

Here is a basic search form, based off what i was using, need to create and/or change form action=searching.php

 
<html>
<body>
<form action="searching.php" method="GET">
Search For:
<br>

state:<input name="state" type="text" size="2"><br>
 
<input type=submit value="Search">

</form>
</body>
</html>
 

on the section where it says print $row just change and add which fields you want filled eg… direction change to first name and etc, just keep adding fields.

[php]

<?php $con = mysql_connect("xxlocalhostxx","xxusernamexx","xxpasswordxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxDBNamexx", $con);

$RESULT = MYSQL_QUERY( "SELECT state FROM xxtablenamexx WHERE 1 LIMIT 0, 30 ":wink:

$where = “”; if($_GET[“address”] != “”)
$where.= ($where == “”?"":" and “).”
address=’".addslashes
(urldecode($_GET[“address”]))."’";
if($_GET[“city”] != “”) $where.= ($where == “”?"":" and “).”
city=’".addslashes
(urldecode($_GET[“city”]))."’";
if($_GET[“street”] != “”) $where.= ($where == “”?"":" and “).”
street like ‘%".addslashes
(urldecode($_GET[“street”]))."%’";
$result = mysql_query(“SELECT * FROM dataentry “.($where==””?"":
(" WHERE “.$where)).” LIMIT 0, 30");

if ($row = mysql_fetch_array($result)) {
do {
print $row[“address”];
print (" “);
print $row[“direction”];
print (” “);
print $row[“street”];
print (” “);
print $row[“suffix”];
print (” “);
print $row[“city”];
print (” “);
print $row[“state”];
print (”

");
} while($row = mysql_fetch_array($result));

} else {print “No Results found, Try again later.”;}
?>

[/php]

i am a beginner so dont take what i say to be set in stone, but for all intents and purposes that should accomplish what you require, also on the html form you can create a drop down to prevent user input from being wrong, or add more fields to narrow selection.

Thanks for the suggestions !

I’m going to try to use your ideas.

Thanks,

Andrew

the code is kinda dirty, if you need help with it let me know.

Sponsor our Newsletter | Privacy Policy | Terms of Service