Page 1 of 1

Read data from a mysql database in dropdown box

Posted: Wed Feb 29, 2012 8:14 am
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)

Re: Read data from a mysql database in dropdown box

Posted: Wed Feb 29, 2012 8:32 am
by Celauran
Best bet would be to implement an AJAX solution using onchange events in each of the select lists.

Re: Read data from a mysql database in dropdown box

Posted: Mon Mar 12, 2012 5:46 am
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.

Re: Read data from a mysql database in dropdown box

Posted: Sat Mar 31, 2012 10:14 pm
by jonessamentha
hmmmm how can it possible i can't believe.