Insert Multiple Checkbox Value in Database Using PHP

Introduction

 
This article explains how to store multiple checkbox values in one column in a database in PHP.
 
Example
 
In this example, we implement storing multiple checkbox values in a database in PHP. There are certain steps we need to follow as explained below.
 
Step 1
 
Create an HTML form, test_post.php, with multiple checkboxes as shown below.
  1. <html>  
  2. <body>  
  3.    <form action="" method="post" enctype="multipart/form-data">  
  4.    <div style="width:200px;border-radius:6px;margin:0px auto">  
  5. <table border="1">  
  6.    <tr>  
  7.       <td colspan="2">Select Technolgy:</td>  
  8.    </tr>  
  9.    <tr>  
  10.       <td>PHP</td>  
  11.       <td><input type="checkbox" name="techno[]" value="PHP"></td>  
  12.    </tr>  
  13.    <tr>  
  14.       <td>.Net</td>  
  15.       <td><input type="checkbox" name="techno[]" value=".Net"></td>  
  16.    </tr>  
  17.    <tr>  
  18.       <td>Java</td>  
  19.       <td><input type="checkbox" name="techno[]" value="Java"></td>  
  20.    </tr>  
  21.    <tr>  
  22.       <td>Javascript</td>  
  23.       <td><input type="checkbox" name="techno[]" value="javascript"></td>  
  24.    </tr>  
  25.    <tr>  
  26.       <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>  
  27.    </tr>  
  28. </table>  
  29. </div>  
  30. </form>  
  31. <?php  
  32. if(isset($_POST['sub']))  
  33. {  
  34. $host="localhost";//host name  
  35. $username="root"//database username  
  36. $word="";//database word  
  37. $db_name="sub_db";//database name  
  38. $tbl_name="request_quote"//table name  
  39. $con=mysqli_connect("$host""$username""$word","$db_name")or die("cannot connect");//connection string  
  40. $checkbox1=$_POST['techno'];  
  41. $chk="";  
  42. foreach($checkbox1 as $chk1)  
  43.    {  
  44.       $chk .= $chk1.",";  
  45.    }  
  46. $in_ch=mysqli_query($con,"insert into request_quote(technology) values ('$chk')");  
  47. if($in_ch==1)  
  48.    {  
  49.       echo'<script>alert("Inserted Successfully")</script>';  
  50.    }  
  51. else  
  52.    {  
  53.       echo'<script>alert("Failed To Insert")</script>';  
  54.    }  
  55. }  
  56. ?>  
  57. </body>  
  58. </html>  
Step 2
 
Select multiple checkboxes as shown below.
 
 
Step 3
 
Now click on the submit button and a popup will be shown for confirmation as shown below.
 
 
Output
 


Similar Articles