View Full Version : The best way to get a drop down field to show data from a MySQL database

09-03-2009, 12:14 PM

I'm a little new to PHP and server side programming in general. But I was looking for some advice of what is the best way to get multiple drop down fields from within a search form display data from a MySQL database.

And once the user has selected from the multiple fields and they have clicked "submit" for the user to be taken to a new page displaying the search results relating to their query.

And help/advice with this would be appreciated.

Many thanks

09-03-2009, 12:32 PM
If I read you right, this is more a HTML problem. Read up on HTML forms and work out which method of multiple selections input you'd like (checkboxes, list) depending on how many options there are.

Build the form as normal, when it's submitted, the receiving script can then use the varialbes passed from the form. With multiple selections you need to explode (PHP function) as these values will be comma seperated. From the array you can build your database query, same as normal.

09-03-2009, 02:58 PM
Basically, the way to fill a dropdown from MySQL is this:


$query = "SELECT * FROM `table`";
$result = mysql_query($query) or die(mysql_error());

print "<select name=\"mySelect\">";

while($row = mysql_fetch_assoc($result))
<option value="<?php print $row['id']; ?>"><?php print $row['value']; ?></option>

print "</select>";


Hope this helps,