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
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--]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.