# Populating drop down list with mysql data

**URL:** <https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461>\
**Category:** programming\
**Created:** [May 15, 2008, 8:31pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461 "2008-05-15T20:31:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kay1021](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@Kay1021](https://forum.kirupa.com/u/Kay1021)\
**Post date:** [May 15, 2008, 8:31pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/1 "2008-05-15T20:31:23Z")

</div>

I’m trying to insert information into my databased

I know how to do this using a form and text fields…but i was wondering if someone could help me with how to make a drop down list that is populated with all the information in a field.

the table i’m grabbing from looks like this

Table: category  
Field1: sub\_cat\_id  
Field2: sub\_cat\_name

I want the drop down to be filled with the category names

I’ve searched for several tutorials and tried a lot of them but i either end up with an empty drop down or the code appearing on the screen.

```
PHP Code:
[LEFT] [COLOR=#000000] [COLOR=#0000BB][/COLOR][COLOR=#007700]&lt;[/COLOR][COLOR=#0000BB]html[/COLOR][COLOR=#007700]&gt;

```

\<[/COLOR][COLOR=#0000BB]head[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]title[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Add Vendor[/COLOR][COLOR=#007700]\</[/COLOR][COLOR=#0000BB]title[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]head[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]body bgcolor[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“white”[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]form method[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“POST” [/COLOR][COLOR=#0000BB]action[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“vendor\_add.php”[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]table[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]col span[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“1” [/COLOR][COLOR=#0000BB]align[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“right”[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Company Name[/COLOR][COLOR=#007700]:\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“name” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Company Reference [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]name with no spaces[/COLOR][COLOR=#007700]):\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“vendor\_ref” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Address [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]ex[/COLOR][COLOR=#007700]. [/COLOR][COLOR=#0000BB]1234 Main St[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Brampton[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Ontario[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Canada[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]L1V 3K4[/COLOR][COLOR=#007700]):\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“addr” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Phone [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]inc[/COLOR][COLOR=#007700]. [/COLOR][COLOR=#0000BB]area code[/COLOR][COLOR=#007700]):\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“phone” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Email[/COLOR][COLOR=#007700]:\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“email” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Website [/COLOR][COLOR=#007700](if [/COLOR][COLOR=#0000BB]available[/COLOR][COLOR=#007700]):\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“website” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Category[/COLOR][COLOR=#007700]:\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>

— [/COLOR][COLOR=#0000BB]THIS IS WHERE I WANT THE DROP DOWN [/COLOR][COLOR=#007700]—

\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]font color[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“blue”[/COLOR][COLOR=#007700]\>[/COLOR][COLOR=#0000BB]Promo[/COLOR][COLOR=#007700]:\</[/COLOR][COLOR=#0000BB]font[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“text” [/COLOR][COLOR=#0000BB]name[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“promo” [/COLOR][COLOR=#0000BB]size[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]100[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\<[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>  
\<[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>\<[/COLOR][COLOR=#0000BB]input type[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“submit” [/COLOR][COLOR=#0000BB]value[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]“Submit”[/COLOR][COLOR=#007700]\>\</[/COLOR][COLOR=#0000BB]td[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]tr[/COLOR][COLOR=#007700]\>

\</[/COLOR][COLOR=#0000BB]table[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]form[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]body[/COLOR][COLOR=#007700]\>  
\</[/COLOR][COLOR=#0000BB]html[/COLOR][COLOR=#007700]\>  
[/COLOR][COLOR=#0000BB][/COLOR] [/COLOR] [/LEFT]

If i enter code like this one below…into the spot designated above…it just prints part of it onto the screen. I don’t know if this is written wrong…of i’m putting it in the wrong spot.

```php

<? 
$query="SELECT sub_cat_id, sub_cat_name FROM category";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=category value=''></option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[sub_cat_id]>$nt[sub_cat_name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>

```

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [May 15, 2008, 8:53pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/2 "2008-05-15T20:53:02Z")

</div>

```php

echo "<select name=category value=''></option>";

```

is what’s wrong … remove the last \</option\>

---

<div class="post-metadata">

**Author:** ![Kay1021](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@Kay1021](https://forum.kirupa.com/u/Kay1021)\
**Post date:** [May 15, 2008, 10:29pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/3 "2008-05-15T22:29:14Z")

</div>

Thanks i removed that…but it still shows all the text boxes and then under category prints this

```auto
 "; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "$nt[sub_cat_name]"; /* Option values are added by looping through the array */ } echo "";// Closing of list box ?>

```

Exactly that…

---

<div class="post-metadata">

**Author:** ![Kay1021](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@Kay1021](https://forum.kirupa.com/u/Kay1021)\
**Post date:** [May 16, 2008, 4:15am UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/4 "2008-05-16T04:15:13Z")

</div>

I finally figured it out!

I was just wondering if there was a way to remove duplicate category names from the drop down box…i need them in the database…but why should the user see duplicates in the drop down

THANKS!

---

<div class="post-metadata">

**Author:** ![kolkata\_dev](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@kolkata\_dev](https://forum.kirupa.com/u/kolkata_dev)\
**Post date:** [May 16, 2008, 12:36pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/5 "2008-05-16T12:36:12Z")

</div>

Please change ur php code part

///////////////////////////////////////

\<?  
$query=“SELECT sub\_cat\_id, sub\_cat\_name FROM category”;

/\* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order \*/

$result = mysql\_query ($query);  
echo “\<select name=category\>”;  
// printing the list box select command

while($nt=mysql\_fetch\_array($result)){//Array or records stored in $nt  
echo “\<option value=’”.$nt[‘sub\_cat\_id’]."’\>’".$nt[‘sub\_cat\_name’]."’\</option\>";  
/\* Option values are added by looping through the array \*/  
}  
echo “\</select\>”;// Closing of list box  
?\>

///////////////////////////////////////

Thanks

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [May 16, 2008, 1:17pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/6 "2008-05-16T13:17:58Z")

</div>

[QUOTE=Kay1021;2326889]Thanks i removed that…but it still shows all the text boxes and then under category prints this

```auto
 "; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "$nt[sub_cat_name]"; /* Option values are added by looping through the array */ } echo "";// Closing of list box ?>

```

[/QUOTE]

[QUOTE=Kay1021;2326977]I finally figured it out![/QUOTE]

must have had a typo in your code then

[QUOTE=Kay1021;2326977]I was just wondering if there was a way to remove duplicate category names from the drop down box…i need them in the database…but why should the user see duplicates in the drop down

THANKS![/QUOTE]

Why do you need duplicate categories?

---

<div class="post-metadata">

**Author:** ![Kay1021](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@Kay1021](https://forum.kirupa.com/u/Kay1021)\
**Post date:** [May 16, 2008, 4:35pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/7 "2008-05-16T16:35:11Z")

</div>

It’s the only way I could figure out how to make sub categories to appear in more than one main category. It may not be ideal but it works for me.

yah it was a little typo…and it almost works. Sucks I am so close but not close enough.

Now the categories in the drop down are words like “Car Wash” so they can have more than one word (& i need the spaces because they appear on screen). When i use this code and select the category and using the code below. It’s adding the correct category to the database…but instead of Car Wash it only puts Car…which is no good.

Is there any way around this?

```php
$sql2="INSERT INTO postalcode (vendor_ref, cat_name, postcode)
VALUES
('$_POST[vendor_ref]','$_POST[cat_name]','$_POST[postcode]')";

```

Thanks

---

<div class="post-metadata">

**Author:** ![actionAction](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/actionaction/32/3987_2.png) [@actionAction](https://forum.kirupa.com/u/actionAction)\
**Post date:** [May 16, 2008, 5:03pm UTC](https://forum.kirupa.com/t/populating-drop-down-list-with-mysql-data/260461/8 "2008-05-16T17:03:17Z")

</div>

You should use:

“SELECT DISTINCT sub\_cat\_name, sub\_cat\_id FROM category ORDER BY sub\_cat\_name”

This will select unique names (i.e. no duplicate names) and will order by name (you can remove that part if you want, but alphabetical lists are nice.
