Populating drop down list with mysql data

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]<[/COLOR][COLOR=#0000BB]html[/COLOR][COLOR=#007700]>

<[/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.


<? 
$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!


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

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

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

 "; // 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…

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!

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

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

 "; // 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?

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?

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

Thanks

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.