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.
- <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 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.
Nkosingizwile NdlovuPosted Feb 12, 2021, 2:16 PM
When i use the code i get an error that says Field..... does not have a default value, and if i set a default value it doesn't update.
shweta mudgilPosted May 6, 2019, 3:54 AM
Hello i used this code but stored values in database are on,on,on???
Tomas FurosPosted Sep 27, 2018, 6:37 AM
Thank you, it works really excellent! (even to a remote mysql-database in another server). My question is if you possibly could re-write how this script would work with PHP PDO instead of mysqli?
Rajesh BishnoiPosted Feb 10, 2017, 9:43 PM
How we select these value in checkbox .Pls tell me
season seasonPosted Jun 5, 2016, 7:16 AM
hows it ?
season seasonPosted Jun 5, 2016, 7:15 AM
some checkboxes value store in datbase then same value get in database ckechbox are checked
Vishal ChaturvediPosted Apr 30, 2016, 4:01 AM
u can retrieve the value from database using select query,after that explode the result on the basis of ' , ' (comma),using explode function and print the array according to index you will get the last value.
Rahul RulesPosted Apr 26, 2016, 1:16 PM
Working perfectly , the best one i saw this among othe modules.and i have doubt , how to retive the values sored in the database which is current value (last one) ex: PHP,.Net,Java, PHP,Java,javascript, .Net,Java, in the above example "PHP,.Net,Java," is the first inserted value, followed by 2nd one , and then third value ".Net,Java," So, i want to print the 3r value which is current one. can u please help me!!! Thank-you !!!
Vishal ChaturvediPosted Feb 2, 2016, 12:22 PM
thnkx adejumo samson
adejumo samsonPosted Jan 31, 2016, 11:20 AM
good of you
Vishal ChaturvediPosted Nov 5, 2015, 7:34 AM
Welcome..!!
dhareppa metriPosted Oct 15, 2015, 3:57 AM
thank you so much
Vishal ChaturvediPosted Mar 17, 2015, 2:52 AM
thank you sir...!!!
Rahul Kumar SaxenaPosted Mar 17, 2015, 1:00 AM
Good Show..