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)

New User

5+ Year Member

joined:Jan 26, 2008
votes: 0

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)

New User

5+ Year Member

joined:Sept 9, 2007
votes: 0

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)

New User

5+ Year Member

joined:Jan 26, 2008
votes: 0

that didn't help. i have the database connected with a connect.php
7:41 am on Jan 27, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 22, 2005
votes: 0

<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)

New User

5+ Year Member

joined:Nov 26, 2006
votes: 0

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]


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members