JDT |
John Dixon |
Populate a Drop Down List Box from a MySQL Database Table |
||||
|
Quite often when you are developing web sites or applications it is necessary to create a drop down list box that contains entries from a database table. You can, of course, hard code the entries in the drop down list box, but this isn't really very elegant, and can create problems if the entries in the database table change. It is much better to extract the entries from the database and then populate the drop down list box using those entries. Creating the database tableThe following script, script.sql, could typically be used to create a database table (called 'years') that contains, for example, a list of years. These will be the entries that appear in the drop down list box. script.sql create table years insert into years (yearID, year) values ('1', '2007-2008'); The following PHP script (script.php), which would form part of a form, will extract all the entries from the 'years' database table, and use them to populate the drop down list box. script.php
<?php If the entries in the database table change at some stage in the future, those changes will be automatically reflected in the drop down list box in the form. Author: John Dixon Go back to MySQL Tutorials home page Go back to Tutorials home page
|
|
|
|||||
|
© 2007-2009 - John Dixon Technology Ltd |
|||||