How to insert Multiple select dropdown value in PHP & MySQL
- Tech Area
- February 7, 2024
In this tutorials, We will learn how to insert multiple select dropdown value into database using PHP and MySQL.
Files used in this tutorial:
1- connection.php (database connection file)
2- index.php (registration form with multiple select dropdown)
Below are the step by step process of how to select and insert multiple select dropdown value in PHP and MySQL.
Step 1: Create a Database connection
In this step, create a new file connection.php to create database connection.
connection.php
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
echo("connection terminated");
}
?>
Step 2: Create Registration form
In this step, create a new file index.php. This is the main file used to create registration form with multiple select dropdown and insert value into database.
This screenshot shows the registration form with multiple select dropdown value.
index.php
<html>
<head>
<title>Registration Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/select2.min.css">
</head>
<style>
.box
{
width:100%;
max-width:600px;
background-color:#f9f9f9;
border:1px solid #ccc;
border-radius:5px;
padding:16px;
margin:0 auto;
}
.error
{
color: red;
font-weight: 700;
}
</style>
<?php
include('connection.php');
if(isset($_REQUEST['register']))
{
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = implode(", ",$_REQUEST['subject']);
$insert_query = mysqli_query($connection, "insert into tbl_registration set name='$name', emailid='$email', subject='$subject'");
if($insert_query>0)
{
$msg = "Data inserted successfully";
}
else
{
$msg = "Error!";
}
}
?>
<body>
<div class="container">
<div class="table-responsive">
<h3 align="center">Registration Form</h3><br/>
<div class="box">
<form method="post" >
<div class="form-group">
<label for="name">Enter Your Name</label>
<input type="text" name="name" id="name" placeholder="Enter Name" required class="form-control"/>
</div>
<div class="form-group">
<label for="email">Enter Your Email</label>
<input type="email" name="email" id="email" placeholder="Enter Email" required class="form-control"/>
</div>
<div class="form-group">
<label for="subject">Select Subject</label>
<select name="subject[]" multiple required class="select form-control">
<option>English</option>
<option>Math</option>
<option>Hindi</option>
<option>Science</option>
<option>Computer</option>
</select>
</div>
<div class="form-group">
<input type="submit" id="register" name="register" value="Submit" class="btn btn-success" />
</div>
<p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
</form>
</div>
</div>
</div>
</body>
</html>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/app.js"></script>
<script src="js/select2.min.js"></script>
Download Source Code
Join 10,000+ subscriber