Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Searching MySQL DB using Drop Menu in PHP

Need a little help getting it to call the information.



9:05 pm on Jan 26, 2008 (gmt 0)

5+ Year Member

I am having trouble getting the DB to call the information back to the page. I am calling information from an abbreviation such as a state to get all the information back to display. Here is part of the code as follows:

<form name=\"state_search\" action=\"search/state_search.php\" method=\"post\">

<p>Please choose your state: </p>

<SELECT name=\"state_search\" size=\"1\" HEIGHT = \"22\" WIDTH = \"50\">
<OPTION value=\"-1\">-</option>

<OPTION value=\"FL\">FL</option>


<input type=\"submit\" />


Here is the state_search.php

as follows:

if ($search)
mysql_connect() or die ("Problem connecting to Database");

$query = "select * from search WHERE state_abbreviation='$search'";

$result = mysql_db_query("state_search", $query);

if ($result)
echo "Here are the results:<br><br>";

while ($r = mysql_fetch_array($result)) { // Begin while
$ts = $r["TimeStamp"];
$state_abbreviation = $r["state_abbreviation"];
$name = $r["name"];
$city = $r["city"];
$zip = $r["zip_code"];
echo "<tr>
<tr> <td colspan=4 bgcolor=\"#ffffa0\">$zip</td>
} // end while
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";


9:36 pm on Jan 26, 2008 (gmt 0)

5+ Year Member

First unless theres part of your script missing you need
$search = $_POST['state_search'] at top of search.php

Mysql_connect contains no arguments so it will only be able to attempt a default connection.

Also mysql_db_query is deprecated - use mysql_select_db .

Basic connect script here [uk.php.net...]


3:11 am on Jan 27, 2008 (gmt 0)

5+ Year Member

that didn't help. i have the database connected with a connect.php


7:41 am on Jan 27, 2008 (gmt 0)

5+ Year Member

<form name=\"state_search\" action=\"search/state_search.php\" method=\"post\">
<SELECT name=\"state_search\" size=\"1\" HEIGHT = \"22\" WIDTH = \"50\">

your form and select have same name which is bad practice though it has nothing to do with your problem.

if ($search){

$search is not defined and has a null value, you should assign $search to whatever you got from the form.
try this it may help.

$search = $_post['state_search'];
//connect to database
mysql_connect("host", "user", "password");
$sql = "SELECT * FROM table WHERE state_abbreviation='$search'";
$result = mysql_query($sql);
while($r = mysql_fetch_array($result){
//do some stuff here


11:42 am on Jan 27, 2008 (gmt 0)

5+ Year Member

A thing to try is in your sql where statement I would try adding a WHERE state LIKE '*".$state."*';

[edited by: jatar_k at 12:53 pm (utc) on Jan. 27, 2008]
[edit reason] no urls thanks [/edit]


Featured Threads

Hot Threads This Week

Hot Threads This Month