Insert Multiple Checkbox Value in Database Using PHP – Info PHP

Introduction

This article explains how to store checkbox values in one column in a 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.php, 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.   

  8.       
  9.   

  10.    
  11.   

  12.    
  13.   

  14.       
  15.   

  16.       
  17.   

  18.    
  19.   

  20.    
  21.   

  22.       
  23.   

  24.       
  25.   

  26.    
  27.   

  28.    
  29.   

  30.       
  31.   

  32.       
  33.   

  34.    
  35.   

  36.    
  37.   

  38.       
  39.   

  40.       
  41.   

  42.    
  43.   

  44.    
  45.   

  46.       
  47.   

  48.    
  49.   

  50. “2”>Select Technolgy:
    PHP“checkbox” name=“techno[]” value=“PHP”>
    .Net“checkbox” name=“techno[]” value=“.Net”>
    Java“checkbox” name=“techno[]” value=“Java”>
    Javascript“checkbox” name=“techno[]” value=“javascript”>
    “2” align=“center”>“submit” value=“submit” name=“sub”>

      

  

  •   
  • 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
    Leave A Reply

    Your email address will not be published.