Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 24th, 2005, 07:35 PM
Authorized User
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Displaying values in dropdown list from mysql db


i was wondering if anybody can help me?

i have a country table that holds 5 different countries. a dropdown list in my form is populated based on this table. the problem is i want the country stored in the member table to be the default selected in the list. this is the code i tried:

<select name=CountryFrm>
$qry = "SELECT CountryName FROM Country ORDER BY CountryName";

$result = mysql_query($qry);
while($row = mysql_fetch_assoc($result))
$CountryNameFrm = $row['CountryName'];

echo '<option value="'.$row[CountryName].'" selected>'.$row[CountryName].'</option>';

Can anybody help

Thanks in Advanced


Old January 25th, 2005, 11:53 AM
Authorized User
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajuru Send a message via Yahoo to rajuru

try replacing this line

echo '<option value="'.$row[CountryName].'" selected>'.$row[CountryName].'</option>';


echo '<option value="'.$row['CountryName'].'" selected>'.$row['CountryName'].'</option>';

Best Regard:
Md. Zakir Hossain (Raju)
Old January 25th, 2005, 12:02 PM
Authorized User
Join Date: Jan 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts

tried that. it didn't work it still displays the last item in the list

Old January 25th, 2005, 12:09 PM
Friend of Wrox
Join Date: May 2003
Posts: 202
Thanks: 0
Thanked 1 Time in 1 Post

The "selected" keyword in the <option> HTML tag controls which value is selected in the drop-down list.

What's missing from your code is a test against your member record for each country to see if that's the one the member has chosen. The one that matches your member record should have the "selected" keyword and the other <option> tags shouldn't.

Bruce Luckcuck
Director, Applications & Support Services
Wiley Publishing, Inc.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a list but not displaying certain values josephine XSLT 2 October 4th, 2007 10:37 AM
populating a dropdown list with values from datab akshay144 VS.NET 2002/2003 5 May 12th, 2006 10:15 AM
displaying images from mySql DB in html cmcmillan.schillers Beginning PHP 1 October 5th, 2004 03:52 AM
Storing multiple values from the dropdown list lily611 SQL Server 2000 0 June 21st, 2004 06:08 AM
Storing multiple values from the dropdown list lily611 ADO.NET 0 June 21st, 2004 05:21 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.