Page 1 of 1

How to get table data in combo box from database

Posted: Thu Jan 12, 2012 10:44 pm
by phpjawahar
Please any one help me on this,
I am new to php.
I want to display my database contents in a combo box. So that users can select only the options i have entered in my database.

advance thanks for your help. Expecting your replies soon.

Jawahar

Re: How to get table data in combo box from database

Posted: Thu Jan 12, 2012 11:26 pm
by califdon
You will need to connect to your database, then send a query to the database that will return the data that you will use in your combo box, then loop through the database result set, and as each value is recovered, your PHP code will use that value as it sends (echos or prints) the appropriate HTML to create the <select> and <option> elements that produce the combo box. If this doesn't make sense to you, I suggest that you first write out what HTML code you would like to see in your browser, then see how you could construct this HTML code with PHP, collecting the data from the database, one piece of data at a time.

The customary loop that is used for such a task is something like this:

Code: Select all

$echo "<select name='mychoice'>";
$con = $sql = 'SELECT choice FROM mytable WHERE .......';
$res = mysql_query($sql) or die(mysql_error);
while($row = mysql_fetch_assoc($sql)) {
    echo "<option value='$row['choice']'>$row['choice']</option>";
}
echo "</select>";
Of course you must substitute the appropriate names for such example names as 'mychoice', 'host', 'user', 'password', 'choice', 'mytable'.

Re: How to get table data in combo box from database

Posted: Thu Jan 12, 2012 11:54 pm
by phpjawahar
Thanks Mr. Califdon. for your reply.

The code is working fine.

One more doubt for me.

I have a form with two fields. One is STATE NAME(combo box) and the second one is CITY NAME(text box).
The user has to select the STATE NAME(combo box) from the form, then enter the CITY NAME(text box).
My question is how to store the CITY NAME entered by the user corresponding to the STATE NAMES, which was selected from the combo box.
When i try this in php the CITY NAME get stored from the first row of my table.

With Regards,
Jawahar

Re: How to get table data in combo box from database

Posted: Fri Jan 13, 2012 11:56 am
by califdon
phpjawahar wrote:I have a form with two fields. One is STATE NAME(combo box) and the second one is CITY NAME(text box).
The user has to select the STATE NAME(combo box) from the form, then enter the CITY NAME(text box).
My question is how to store the CITY NAME entered by the user corresponding to the STATE NAMES, which was selected from the combo box.
When i try this in php the CITY NAME get stored from the first row of my table.
Can you be a little more specific? How are you going to store this data and what else will be stored? I don't quite understand what you are attempting to do. Perhaps if you begin by telling us what your project is, such as "I am developing a user login script, where the user fills in their name and city, and selects their state from a drop-down box, and I will store this in a MySQL database after checking to see if it is a duplicate..." or something like that; then we can help you do that.