Friday, January 22, 2016

PHP MYSQL: Display Data in Combobox

Hi there! Today I am going to create a database-driven combobox/dropdown menu. I will display all records from the CATEGORY table into a combobox/dropdown menu. You often encounter this type of function in creating a dynamic information systems. But before we achieve this dynamic or what we call a database driven combo box or dropdown box, we need to add data in our category table below. I created a tutorial to be able to Insert New Record in the Database.

First step is to create the CATEGORY table with category_id and category_name column names and add your data to it like the one below.


PHP and MySQLi Code
$con = mysqli_connect("localhost","root","","sample");

//display values in combobox/dropdown
$result = mysqli_query($con,"SELECT * FROM category ORDER BY category_name");
while($row = mysqli_fetch_assoc($result))
echo "<option>$row[category_name]</option>";

Insert the above code between your form tag and save it with the file extension of .php. The first line in your php code is the database connection which contains the hostname (localhost), username (root), password ("") since I have no password in mysql, database name (sample).

Output Display Data in Combobox

Feel free to comment below for any questions. God bless you all! Hope this helps! ^_^

PHP MYSQL: Display Data in Combobox

No comments:

Post a Comment