How to Insert Multiple checkbox value in database MySQL using PHP

how to insert multiple checkbox value in database MySQL using PHP

In this tutorial, we are going to learn to insert multiple checkbox values in a database using MySQL and pdo in PHP with examples.

In this example, we are implementing storing multiple checkbox values and the checkbox is used in many forms like school forms, college forms, and company adding skills and knowledge forms, etc. 

 

Read also: How to add html responsive table in blogger and wordpress



Here, we are going to create a database table and empty PHP files.



Insert multiple checkbox value in database MySQL using PHP


CREATE TABLE IN DATABASE:



  create table checkbox
  (
  id int AUTO_INCREMENT PRIMARY KEY,
  language varchar(250) not null
  );
  


Insert multiple checkbox value in database MySQL using PHP



<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
<div class="container py-5">
<h1 class="text-center py-5">Insert multiple checkbox value in database MySQL using PHP</h1>
<form class="form-control" method="post" action="multichk.php">
<h2 class="text-center">Courses</h2><br/>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="English">English
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Tamil">Tamil
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Hindi">Hindi
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Telugu">Telugu 
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Malayalam">Malayalam 
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="kannada">kannada 
</div>
<div class="mb-3 py-3">
<input class="btn btn-primary"  type="submit" value="Submit">
<input class="btn btn-danger"  type="reset" value="Reset">
</div>
<p class="text-center py-3"><strong>dailyaspirants.com</strong></p>
</form> 
</div>
</body>
</html>  
  


PHP FILE(multichk.php)



<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phptut";

$checkbox = $_POST['language'];
    $chk="";  
    foreach($checkbox as $chkb)  
       {  
          $chk.= $chkb.",";  
       }  

$conn = mysqli_connect($servername,$username, $password, $dbname);

if (!$conn) 
{
    
	die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO checkbox(language)VALUES( '$chk' )";

if(mysqli_query($conn,$sql)) {

    echo 'Data added sucessfully';
} 
else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?> 
  


Insert multiple checkbox value in database MySQL using PHP and PDO



<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
<div class="container py-5">
<h1 class="text-center py-5">Insert multiple checkbox value in database MySQL using PHP</h1>
<form class="form-control" method="post" action="pdomulti.php">
<h2 class="text-center">Courses</h2><br/>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="English">English
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Tamil">Tamil
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Hindi">Hindi
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Telugu">Telugu 
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="Malayalam">Malayalam 
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="language[]" value="kannada">kannada 
</div>
<div class="mb-3 py-3">
<input class="btn btn-primary"  type="submit" value="Submit">
<input class="btn btn-danger"  type="reset" value="Reset">
</div>
<p class="text-center py-3"><strong>dailyaspirants.com</strong></p>
</form> 
</div>
</body>
</html>  
  


PHP and PDO FILE(pdomulti.php):


<?php
$servername = "localhost";
$username = "root";
$password = "";

$checkbox = $_POST['language'];
    $chk="";  
    foreach($checkbox as $chkb)  
       {  
          $chk.= $chkb.",";  
       }  
	   
try {
  $conn = new PDO("mysql:host=$servername;dbname=phptut", $username, $password);
  
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "INSERT INTO checkbox(language)VALUES( '$chk' )";
  $conn->exec($sql);
  echo "Data Inserted successfully";
  
} 
catch(PDOException $e) 
{
  echo "Connection failed: " . $e->getMessage();
}
?>  
  


I hope you will learn how to insert multiple checkbox value in database using MySQL using PHP and pdo and the just copy and paste on your editor change your wish.
Previous Post Next Post