homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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)

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)

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)

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


 7:41 am on Jan 27, 2008 (gmt 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)

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]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved