Page 1 of 1

Populating a dropdown/Combo box from MySQL. How to get your database information into a dropdown box. Rate Topic: ***** 1 Votes

#1 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 511
  • View blog
  • Posts: 1,600
  • Joined: 08-November 08

Posted 21 March 2009 - 02:07 PM

Chances are, if you're reading this now, you've been wracking your brain over how to get that pesky combo box to accept your database information. Today, I have a solution for you. I'm going to demonstrate how you can make this happen.

We'll start by assuming we have a table called 'users'. We're using MySQL, and we want a dropdown box containing all of our users from our users table, by name. So, we'll go step by step. I'll assume for the purposes of this tutorial that, prior to this code, the database connection has already been established. The first thing we'll want to do is to run our query to get the information into a result resource. Since we want to display their user names in the dropdown box, we can speed up the operation and use up less memory by pulling only the field that we need.

// Write out our query.
$query = "SELECT username FROM users";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());


Now $result contains the result resource we'll be using to populate our dropdown box.

Fast forward to where we'll actually be doing the HTML for the dropdown box. As you're probably aware, the dropdown box is a select element, and each choice is an option sub-element inside of the select. We can use string concatenation to build our dropdown box, and use the echo construct to output it afterward. We'll name our dropdown box 'users', in keeping with its contents.

$dropdown = "<select name='users'>";
while($row = mysql_fetch_assoc($result)) {
  $dropdown .= "\r\n<option value='{$row['username']}'>{$row['username']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;


You'll notice that our dropdown box is output containing option elements for all of our users. To control that, make the dropdown box part of a form, and do a select statement based on the value of 'usernames', after it has been cleaned and validated (gotta watch out for spoofed pages), and you can make this a select box that control what information gets passed to the form for modifying information on users in your database.

I hope you find this helpful.

Is This A Good Question/Topic? 0
  • +

Replies To: Populating a dropdown/Combo box from MySQL.

#2 wrighty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 09-December 08

Posted 15 May 2009 - 03:31 AM

Any way of getting the value out of this and storing as a variable?
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 511
  • View blog
  • Posts: 1,600
  • Joined: 08-November 08

Posted 20 June 2009 - 09:36 PM

Yes. After the form containing the combo box is submitted, simply use $_GET or $_POST, depending on your preference, just as you would for regular form elements.
Was This Post Helpful? 0
  • +
  • -

#4 N1tr0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 09

Posted 13 April 2010 - 05:00 PM

I am doing something similar but I would like to take it a step further and have been unable to find a good solution, surprisingly.
I can get my dropdown to populate and even update my table but I would like the dropdown to select the current value in the database for that row.
How can I do that?

Thanks,
- N1tr0

This post has been edited by N1tr0: 14 April 2010 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 N1tr0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 09

Posted 14 April 2010 - 10:11 AM

OK, I have it to where the dropdown selects the current value in the database but when I choose a new value and update the record, that field is set to '0' instead of the value I selected. So it looks like an either / or right now. :-/

Any ideas on how to get it to grab the new value?

This post has been edited by N1tr0: 14 April 2010 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 N1tr0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 09

Posted 14 April 2010 - 10:26 AM

OK, I figured it out. Man that feels good. :-)

I now have a form with a dropdown menu that is populated via a lookup table and defaults to the value currently in the master table for that row and field.
I can select a new value from the lookup table (via the dropdown) and have the new value updated on the master table.

Ping me if you need a similar solution. I'd be happy to share it.
Was This Post Helpful? 0
  • +
  • -

#7 Scotty G  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 59
  • Joined: 09-April 10

Posted 14 April 2010 - 10:26 AM

View PostN1tr0, on 14 April 2010 - 09:11 AM, said:

OK, I have it to where the dropdown selects the current value in the database but when I choose a new value and update the record, that field is set to '0' instead of the value I selected. So it looks like an either / or right now. :-/

Any ideas on how to get it to grab the new value?


Sorry, I'm having troubles trying to follow here. I've done something similar my self. Are you having troubles getting the new selected value into your database?
Was This Post Helpful? 0
  • +
  • -

#8 deathmeat  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-August 10

Posted 27 February 2011 - 05:32 AM

View PostValek, on 21 March 2009 - 02:07 PM, said:

Chances are, if you're reading this now, you've been wracking your brain over how to get that pesky combo box to accept your database information. Today, I have a solution for you. I'm going to demonstrate how you can make this happen.

We'll start by assuming we have a table called 'users'. We're using MySQL, and we want a dropdown box containing all of our users from our users table, by name. So, we'll go step by step. I'll assume for the purposes of this tutorial that, prior to this code, the database connection has already been established. The first thing we'll want to do is to run our query to get the information into a result resource. Since we want to display their user names in the dropdown box, we can speed up the operation and use up less memory by pulling only the field that we need.

// Write out our query.
$query = "SELECT username FROM users";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());


Now $result contains the result resource we'll be using to populate our dropdown box.

Fast forward to where we'll actually be doing the HTML for the dropdown box. As you're probably aware, the dropdown box is a select element, and each choice is an option sub-element inside of the select. We can use string concatenation to build our dropdown box, and use the echo construct to output it afterward. We'll name our dropdown box 'users', in keeping with its contents.

$dropdown = "<select name='users'>";
while($row = mysql_fetch_assoc($result)) {
  $dropdown .= "\r\n<option value='{$row['username']}'>{$row['username']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;


You'll notice that our dropdown box is output containing option elements for all of our users. To control that, make the dropdown box part of a form, and do a select statement based on the value of 'usernames', after it has been cleaned and validated (gotta watch out for spoofed pages), and you can make this a select box that control what information gets passed to the form for modifying information on users in your database.

I hope you find this helpful.

nothing is being displayed in the combo box:S
Was This Post Helpful? 0
  • +
  • -

#9 okayRICE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-July 11

Posted 25 July 2011 - 08:41 AM

View PostValek, on 20 June 2009 - 09:36 PM, said:

Yes. After the form containing the combo box is submitted, simply use $_GET or $_POST, depending on your preference, just as you would for regular form elements.


I know this is pretty late to the party, but I'm having some trouble returning a variable from the drop table.
echo "<form action='edit_data.php?page=3' method='post'>";
						$dropdown = "<select name='issue_table'>";
						while($row = mysql_fetch_assoc($result)) {
						$dropdown .= "\r\n<option value='{$row['pcb_id']}'>{$row['pcb_id']}</option>";
						}
						$dropdown .= "\r\n</select>";
						echo $dropdown; 
						echo "<input type='submit' name='filter' value='Filter' />";
						echo "</form>";


Could you give me an example of what I need to change to return the selected value with $POST?
Was This Post Helpful? 0
  • +
  • -

#10 Kaybin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 08

Posted 23 November 2011 - 07:58 AM

What if you would like the Option Captions to output related information from another table? For example. The value would be an indexed primary key (int). Say the value corresponds to a reference table. For example the values would be 1,2,3,4 but I would like to take that 1-4 and associate via a different table which would pull (Color and Code) Red, Blue, Yellow, Green and 101,102,103,104. The option box should read off ass Red - 101 or Blue - 202.

My problem is my code seems to be breaking my combobox. The snippet of code works fine outside of the combobox so I know it is simply the placement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1