Read data from a mysql database in dropdown box

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
yomi
Forum Newbie
Posts: 1
Joined: Thu Feb 23, 2012 8:54 am

Read data from a mysql database in dropdown box

Post by yomi »

Hello, I'm new on this forum
I have a small question on connecting mysql / php
I would like to display the contained of many different table from the same database in multiple dropdown boxs with a connection between them (example: choose a selection in the first box and it changes the contained the second box)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Read data from a mysql database in dropdown box

Post by Celauran »

Best bet would be to implement an AJAX solution using onchange events in each of the select lists.
GQAH
Forum Newbie
Posts: 1
Joined: Fri Mar 09, 2012 6:01 am

Re: Read data from a mysql database in dropdown box

Post by GQAH »

For displaying database items I use this code :

Code: Select all

        mysql_connect("localhost", "", "") or die(mysql_error());
        mysql_select_db("pop_security") or die(mysql_error());
        $query="SELECT id FROM pop";
        /* 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=delete value=''>Student Name</option>";
        // printing the list box select command
        while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
        echo "<option value=$nt[id]>$nt[id]</option>";
        /* Option values are added by looping through the array */
        }
        echo "</select>";// Closing of list box ?>
The first $nt is the item to store in the database, the second one is the item to be displayed.
In this code the ID will be stored en displayed. But when you use :

Code: Select all

echo "<option value=$nt[id]>$nt[id]</option>";
The ID will be stored, but the name will be displayed.
jonessamentha
Forum Newbie
Posts: 1
Joined: Sat Mar 31, 2012 10:10 pm

Re: Read data from a mysql database in dropdown box

Post by jonessamentha »

hmmmm how can it possible i can't believe.
Post Reply