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.php, with multiple checkboxes as shown below.
- <html>
- <body>
- <form action="" method="post" enctype="multipart/form-data">
- <div style="width:200px;border-radius:6px;margin:0px auto">
- <table border="1">
- <tr>
- <td colspan="2">Select Technolgy:</td>
- </tr>
- <tr>
- <td>PHP</td>
- <td><input type="checkbox" name="techno[]" value="PHP"></td>
- </tr>
- <tr>
- <td>.Net</td>
- <td><input type="checkbox" name="techno[]" value=".Net"></td>
- </tr>
- <tr>
- <td>Java</td>
- <td><input type="checkbox" name="techno[]" value="Java"></td>
- </tr>
- <tr>
- <td>Javascript</td>
- <td><input type="checkbox" name="techno[]" value="javascript"></td>
- </tr>
- <tr>
- <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>
- </tr>
- </table>
- </div>
- </form>
- <?php
- 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'<script>alert("Inserted Successfully")</script>';
- }
- else
- {
- echo'<script>alert("Failed To Insert")</script>';
- }
- }
- ?>
- </body>
- </html>
Step 2Select multiple checkboxes as shown below.
Step 3Now click on the submit button and a popup will be shown for confirmation as shown below.
Output