Jump to content

More select boxes with db input


Recommended Posts

G'day

I love this forum ...great place for noobie like me to pick up pointers & REAL help.

 

If I have a list /menu how can I populate it with the results from a query.

 

Theres about 40 possible values so far

eg bob, bill, mary etc & growing all the time.

 

There must be a simpler way than adding to the list of options.

 

Assume the db is test

table is product

field is name

 

Thanks in advance

Ken

Link to comment
https://forums.phpfreaks.com/topic/2275-more-select-boxes-with-db-input/
Share on other sites

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?

[/span][span style=\"color:#FF8000\"]//database like this

// id     name

//--------------

// 1      Dog

// 2      Fish

// 3      Cat

 

[/span][span style=\"color:#0000BB\"]mysql_connect[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"localhost\"[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\"user\"[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\"password\"[/span][span style=\"color:#007700\"]);

[/span][span style=\"color:#0000BB\"]mysql_select_db[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"test\"[/span][span style=\"color:#007700\"]);

[/span][span style=\"color:#0000BB\"]$res[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"SELECT * FROM product\"[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]());

echo [/span][span style=\"color:#DD0000\"]\"<select name=myselect>\"[/span][span style=\"color:#007700\"];

while([/span][span style=\"color:#0000BB\"]$row[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$res[/span][span style=\"color:#007700\"])) {

  echo [/span][span style=\"color:#DD0000\"]\"<option value=$row[id]>$row[name]</option>\"[/span][span style=\"color:#007700\"];

}

echo [/span][span style=\"color:#DD0000\"]\"</select>\"[/span][span style=\"color:#007700\"];

[/span][span style=\"color:#0000BB\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.