Insert Multiple Checkbox Value in Database Using PHP – Info 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 a HTML form, test_post., with multiple checkboxes as shown below.

  1.   
  2.   
  3.    “” method=“post” enctype=“multipart/form-data”>  
  4.    “width:200px;border-radius:6px;margin:0px auto”>  
  5. “1”>  
  6.    
      
  7.       “2”>Select Technolgy:

      

  8.    

      

  9.    
      
  10.        PHP

      

  11.        “checkbox” name=“techno[]” value=“PHP”>

      

  12.    

      

  13.    
      
  14.        .Net

      

  15.        “checkbox” name=“techno[]” value=“.Net”>

      

  16.    

      

  17.    
      
  18.        Java

      

  19.        “checkbox” name=“techno[]” value=“Java”>

      

  20.    

      

  21.    
      
  22.        Javascript

      

  23.        “checkbox” name=“techno[]” value=“javascript”>

      

  24.    

      

  25.    
      
  26.       “2” align=“center”>“submit” value=“submit” name=“sub”>

      

  27.    

      

  28.   

  

  •   

  • if(isset($_POST[‘sub’]))  
  • {  
  • $host=“localhost”;  
  • $username=“root”  
  • $word=“”;  
  • $db_name=“sub_db”;  
  • $tbl_name=“request_quote”  
  • $con=mysqli_connect(“$host”“$username”“$word”,“$db_name”)or die(“cannot connect”);  
  • $checkbox1=$_POST[‘techno’];  
  • $chk=“”;  
  • foreach($checkbox1 as $chk1)  
  •    {  
  •       $chk .= $chk1.“,”;  
  •    }  
  • $in_ch=mysqli_query($con,“insert into request_quote(technology) values (‘$chk’)”);  
  • if($in_ch==1)  
  •    {  
  •       echo‘‘;  
  •    }  
  • else  
  •    {  
  •       echo‘‘;  
  •    }  
  • }  
  • ?>  
  •   
  •  
  • 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

     

     

    Article Prepared by Ollala Corp

    You might also like More from author

    Leave A Reply

    Your email address will not be published.