How to get values from multiple select dropdown in PHP and MySQL

A multiple select dropdown is a type of input field with option to select one or more values from a list of values. When a user submits a form with multiple selected values, you have to store the values in the database. We will show how to handle form submission with multiple selected values in a form in PHP and MySQL.

For a usual select dropdown, user can select only one value from the list. Selected value is taken in a variable and insert/update takes place in the database. For multiple select dropdown, you have to handle more than one value from the list. So, you have to use an array of values and update/insert database accordingly.

Select Dropdown

Multi-Select Dropdown

For multi-select, field name is an array - skill[ ] and "multiple" attribute is used.

In this topic I will develop an application in PHP and MySQL, where user enter name, email and select one or more skills and submit a form. So the skill is our multi-select dropdown. The screenshot of the form is given below:

Form for multi-select dropdown in PHP

select multiple options in dropdown in PHPFolders and Files

Below is a screenshot of the folder structure and files I am using:

show multiple selected values in dropdown in PHP

I have created a folder called 'multi_select' under 'htdocs' as I am using XAMPP. If you are using WAMP, create 'multi_select' folder under 'www'.

Folder 'cfg' - In this folder I have kept dbconnect.php which is used to connect to the MySQL database.
Folder 'css' - My custom stylesheet is in this folder.
Folder 'db' - In this folder I have kept my create table scripts.

I have two php programs, one is index.php which is the form to take input data from the user and other is process_skills.php which is used to process the form submitted by the user. Actually, I have included process_skills.php within index.php. Below is a screenshot of the form:

PHP Multiple Select Dropdown Example

Step 1 - Create MySQL tables

We will create three tables - skills, candidate and candidate_skills. I have a database called 'demo'. So the tables will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same tables do not exist already. Table structures are given below:

Table 1 - skills

skills table is used to store all skills with id and name. This is a master table to show the list of available skills. Below is the structure of the table:

Multiple Select Dropdown and Get Values in PHP

This table has 2 columns.

  1. skill_id - it is the primary key and auto incremented
  2. skill_name - name of the skill
Create table script for this table is given below, you can use this code to create the table. You can also download all scripts from download section later in this topic.

db/skills.sql

After you create the table and inserted data in it, verify if data is inserted correctly, it should have below data in it.

Multiple Select DropDown List Tutorial in PHP

Table 2 - candidate

candidate table is used to store candidate details like name, email id etc. Below is the structure of the table:

multiple selection in dropdown in php and mysql

This table has 4 columns.

  1. candidate_id - it is the primary key and auto incremented
  2. name - name of the candidate entered by the user in the form
  3. email_id - email id of the candidate entered by the user in the form
  4. registration_dt - Date on which candidate was registered
Create table script for this table is given below, you can use this code to create the table. You can also download and get the scripts.

db/candidate.sql

I have kept this table empty initially. When user submits the form, this table gets populated.

Table 3 - candidate_skills

candidate_skills table is used to store skills for each candidate. When user registers with their skills, then all skills entered for a user are stored in this table, candidate wise. So, one or more rows will be in this table for each candidate. Below is the structure of the table:

display multiple selected value of dropdownlist in php

This table has 3 columns.

  1. id - it is the primary key and auto incremented
  2. candidate_id - candidate_id from candidate table. You can say it is the foreign key of candidate table, though I have not enforced constraints here.
  3. skill_id - skill_id of skill for a candidate

db/candidate_skills.sql

This table is also empty initially. When user submits the form, this table gets populated.

Step 2 - Connect to MySQL database (dbconnect.php)

Use below script to connect to the database. Note that we have the database connection php program in 'cfg' folder. This is written once and used in every program where database connection is needed. This will be easy for maintenance and also will enable reusability of the code.

dbconnect.php

I am using mysqli_connect() function which needs 4 parameters.

These four parameters are:

  1. server - in our case it is localhost
  2. userid - we are using root user
  3. password - no password for user root
  4. database name - demo in our case.

We will include this dbconnect.php in other php programs so that we do not need to write it again in the program. For detail database connection understanding please read topic How to Connect to MySQL database in PHP using mysqli_connect.

Step 3 - Create a form for skill entry (multi-select) (index.php)

The form has a multi-select input field, where user can select one or more skills from a list. The screenshot is given below:

php multiple select option from database

Let's see the code for this.

index.php

It is a simple form where user can enter Name, Email id and select Skills. Skill is a multiple select dropdown, user can choose one or more skills. You can see we are selecting all skills from the "skills" table to populate this multi-select list box. Remember that values selected in this multi-select list box will be in an array named skill[]. Once the form is submitted, process_skills.php will process the data.

Step 4 - Write PHP code to process form data (process_skills.php)

It validates the email and inserts a row in candidate table. Also, inserts rows in candidate_skills table for selecetd skills for that candidate. It processes the skills as an array, so uses a for loop. See the code for process_skills.php below:

process_skills.php

See in line 27, $last_id is the last inserted id of the candidate (candidate_id), using mysqli_insert_id($conn) you can get the id of the inserted row in a table.

Hope you could understand how multi-select is applied in this application.

Step 5 - Add CSS (style.css)

Let us add below styles. I have already added style.css in index.php.

css/style.css

You can see simple styles are added here. Keep this style.css file in your css folder.

Create Multi Select Dropdown in PHPTest the Application

Make sure in your XAMPP control panel Apache and MySQL services are running. Open the browser and run localhost/multi_select. You will see the form is displayed. See the below screen:

Handling multiple selection drop down list box in php

Enter Name, Email and select more than one skills and submit. You will see successful message displayed. See below:

insert multiple select dropdown option in php

creating Multiple Selection Lists in PHP

Verify that rows are inserted in candidate and candidate_skills tables in the database.

Just for validation purpose, you can test by giving wrong email id format and same email id again to see if error message are displayed.

Get multiple Selected Values in PHPImportant Note

  1. I used in_array() php function to check if a value is already in the selected array, so that user does not have to select select skills again in case it gives some error during submission.
  2. Since this application is using multiple database inserts in more then one table, it is always better to use database commit and rollback explicitly. Because if one insert fails we need to rollback to enure data consistecy. Please read the topic How to implement commit and rollback in PHP and MySQL

Get HTML Multi-Select Values in PHPDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

Insert Multiple Values Using Dropdown List Box in PHPConclusion

Multi-Select is a very common requirement, so you must know how to use it in a form and process the selected values. I have tried to explain it by developing this simple application. Hope it will be useful for you.