Hello everybody today i will show you how to populate an dropdown. Populating an select is simple task in PHP. In this tutorial I am going to show you the method with mysqli.

Table Creation in PhpMyAdmin

First step is to create a table, I recommend to use phpmyadmin, because it is very simple to use.

PhpMyAdmin Table Creation.

I set number of columns to 2, because it is enough for this example. I recommend you to name the first column “id” and set the AutoIncrement and PrimaryKey Attribute. I called the second column “name” and set the type to TEXT. You can name the columns however you want, but I really recommend naming the first id. You should have entered the information like shown below.

And now press Save button! Now let’s go to the code! Open some text editor or some IDE, I recommend PhpStorm (not paid promotion). Create new .php file. Write in there these lines

The Code

<form>
    <select name="dropdown" id="dropdown">
<?php
$db = mysqli_connect("host", "username", "password", "database");
$res = mysqli_query($db, "SELECT * FROM sometable");
while($row = mysqli_fetch_array($res)) {
    echo("<option value='".$row['id']."'>".$row['name']."</option>");
}
?>
<label for="dropdown">Select</label>
</select>
<button type="submit">Submit</button>
</form>

Functions

  • <form> – the HTML element of form.
  • <select> – the HTML element of dropdown.
  • <button> – the HTML button
  • <label> – label for form fields
  • <?php – opening of php
  • mysqli_connect() – connection to database
  • mysqli_query() – sends an query to an database
  • mysqli_fetch_array() – gets data from query

Parameters

Parameters for mysqli_connect() shown below:

  • host
  • username
  • password
  • database
  • [port]

Parameters for mysqli_query() shown below:

  • database link
  • query

Parameter for mysqli_fetch_array() shown below:

  • query

Insert data to table

So we must insert data to database to test it. In the menu at the top, click on the “Insert” button as shown below.

When you press the button, you get redirected to Insert page, It should look like shown below.

There’s no need to fill the id field, because of the AutoIncrement Attribute, but we need to fill out the name field, then press Go. You can repeat this process how many times you want.

Done!

So the final thing should look like this. When you expand the dropdown you should see everything you written to the database. It should look like this. If you want you may design it with CSS, or use it in some form.

Thank you for visiting m-world.cz! I hope this page helped you. If you have any questions or troubles write down to the comments. If you have any other problems go to our forum page, I will try to help you. Sorry for my bad English. Please rate the page above the comment section. Have a great day!

Please rate this


3 Comments

waterfallmagazine.com · 22. July, 2020 at 14:22:37

https://waterfallmagazine.com
I was curious if you ever thought of changing the structure of
your blog? Its very well written; I love what youve got to
say. But maybe you could a little more in the way
of content so people could connect with it better.

Youve got an awful lot of text for only having 1 or
two images. Maybe you could space it out better?

    mkolakovsky · 9. August, 2020 at 16:41:58

    Thanks for the comment!
    This is my first blog post, sorry for bad content structure.
    I’ll try my best to it look better, sorry for my bad english.

Parker · 9. August, 2020 at 15:51:22

Re: How to populate an html dropdown via mysql and PHP.

You saved my ass!

have a great day!

Leave a Reply

Your email address will not be published. Required fields are marked *