How to create multiple entries in database for multiple selected values in checkbox

In this tutorial, we are going to learn how to create multiple entries in a database for multiple selected values in the checkbox and multiple entries into the database in PHP are very useful. If the people have selected some more information to take help of the checkbox to do multiple entries in the form and we have to do that in PHP array[], Now, let's details to create the form and multiple entries in the database.


multiple entries in database for multiple selected values in checkbox


First, we need to create a database and like below:


Create table in MySQL:




CREATE TABLE datafun 
(
  `id` int(11) NOT NULL  Auto_increment primary key,
  `username` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL,
  `prolang` varchar(250) NOT NULL
); 



In the HTML to create a form for the multiple entries and need to action in PHP to store the data in the database. and the code is given below.This is the HTML form to create a checkbox for select multiple values and send to database.


 

  
<div class="mb-4">
    <label class="form-label">Programming Known:</label><br>
    <input type="checkbox" value="java" name="prolang[]">&nbsp;JAVA <br>
    <input type="checkbox" value="php" name="prolang[]">&nbsp;PHP <br>
    <input type="checkbox" value="python" name="prolang[]">&nbsp;PYTHON<br>
    <input type="checkbox" value="javascript" name="prolang[]">&nbsp;JAVASCRIPT</td>
</div>



Create HTML Form:



  
<div class="container py-5">
<form class="form-control space-form py-5" action="datafun.php" method="POST">
<div class="row row-space">
<div class="mb-4">
    <label class="form-label">Name:</label>
	<div class="col-sm-12">
	<input type="text" class="form-control" id="exampleInputEmail1" name="username" required>
	</div>
</div>
<div class="mb-4">
    <label class="form-label">Email:</label>
	<div class="col-sm-12">
	<input type="email" class="form-control" name="email" required>
	</div>
</div>
<div class="mb-4">
    <label class="form-label">Programming Known:</label><br>
    <input type="checkbox" value="java" name="prolang[]">&nbsp;JAVA <br>
    <input type="checkbox" value="php" name="prolang[]">&nbsp;PHP <br>
    <input type="checkbox" value="python" name="prolang[]">&nbsp;PYTHON<br>
    <input type="checkbox" value="javascript" name="prolang[]">&nbsp;JAVASCRIPT</td>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="submit">
</div>
</form>
</div>
  
  


multiple entries in database for multiple selected values in checkbox


PHP code



In the below code to use the multiple entries inserted into database using of multiple selected values in checkbox.


datafun.php




<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname="phptutorial";
$con = new mysqli($servername, $username, $password,$dbname);

if (isset($_POST['submit'])) 
{
$username=$_POST['username'];
$email=$_POST['email'];
$language=$_POST['prolang'];
$data=implode(",",$language);

$sql="INSERT INTO datafun (username,email,prolang) 
VALUES ('".$username."','".$email."','" . $data . "')"; 

if(mysqli_query($con, $sql))
{ 
echo "Data Inserted Successfully";
}
else
{
echo "ERROR WHILE DATA INSERTED";
}
}
?>


Full Code:




<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
.space-form{width:50%; margin:0 auto;}
.row-space{margin:0 auto;}
</style>
</head>
<body>
<h1>How to create multiple entries in database for multiple selected values in checkbox</h1>
<div class="container py-5">
<form class="form-control space-form py-5" action="datafun.php" method="POST">
<div class="row row-space">
<div class="mb-4">
    <label class="form-label">Name:</label>
	<div class="col-sm-12">
	<input type="text" class="form-control" id="exampleInputEmail1" name="username" required>
	</div>
</div>
<div class="mb-4">
    <label class="form-label">Email:</label>
	<div class="col-sm-12">
	<input type="email" class="form-control" name="email" required>
	</div>
</div>
<div class="mb-4">
    <label class="form-label">Programming Known:</label><br>
    <input type="checkbox" value="java" name="prolang[]">&nbsp;JAVA <br>
    <input type="checkbox" value="php" name="prolang[]">&nbsp;PHP <br>
    <input type="checkbox" value="python" name="prolang[]">&nbsp;PYTHON<br>
    <input type="checkbox" value="javascript" name="prolang[]">&nbsp;JAVASCRIPT</td>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="submit">
</div>
</form>
</div>
</body>
</html>


multiple entries in database for multiple selected values in checkbox


Previous Post Next Post