-
Populating a dynamic drop down menu from a MySQL database
Posted on February 13th, 2009 6 comments
This PHP code snippet retrieves the data in a specific column of each row in a MySQL table. The retrieved data is echoed to the DOM and displayed as options of a drop down menu. The options of the drop down menu are dynamic, as they will change automatically every time the information in the database is modified.This is the code:
<? // Make MySQL Connection mysql_connect("localhost", "admin", "pass") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); $sql = "SELECT column_name FROM table"; // Echo echo "<SELECT name="selectmenu">"; $result = mysql_query($sql); // Loop to get and echo the results as options while ($row = mysql_fetch_assoc($result)) { echo '<option value="'.$row["column_name"].'">' .$row["column_name"].'</option>'; } echo "</SELECT>"; ?>With this code your drop down menu should contain as options the data in column_name for each row.
An explanation on the mysql_fetch_assoc function can be found here.
Related posts:
Computers & programming, HTML, MySQL, PHP column, drop down, dynamic, menu, mysql, php, populating, query, row, select, while6 responses to “Populating a dynamic drop down menu from a MySQL database”
-
Hi,
can you help me with the coding for mutiple selection from the drop down list(the data for drop down list is obtained from mysql database) and insert the values into a table for a particular field.
Thanks
-
Unfortunately and because of university stuff, I do not have much free time right now. However, even though I can’t promise it, I will be glad to help so far as it is possible to
Could you give some more details of what you mean? I’m not sure I understood it well… you want to know how to insert to a database the multiple values a user has submitted?
Regards
-
Thanks for the response .Yes I want the multiple options selected in drop down list to be inserted to a database.
Thanks
-
Hello again
Try fetching the selected options with an array.
How to is explained here: http://www.daniweb.com/forums/post755304-10.htmlOn how to save an array to MySQL or retrieve a saved array, you will find very useful information here: http://www.evolt.org/article/Storing_form_array_data_to_MySQL_using_PHP/18/60222/
Regards
-
Thanks
-
This is a working example I made. I’ve decided to cover the topic in my next post.
http://www.aleixcortadellas.com/demos/multipleselection/UPDATE: There you go: http://www.aleixcortadellas.com/main/2009/03/20/492/
Leave a reply
-


