Save Multiple Checkbox values in Database using PHP is the our topic for this tutorial. Many times in our application we need to save the multiple or single checkbox value in the database for further processing like saving use hobbies, saving multiple answers of a question and many more. I have seen many people stuck and many times i got the mail regarding this matter. Finally i have created the video over the youtube and now i am also writing the tutorial for your help. If you have any questions than please comment below. If you want to see the example in live than you can visit my youtube video for saving the multiple checkbox in PHP.
Summary of how we will do this :-
- Create Database with 2 columns Values and Id.
- Create Html page with multiple checkboxes.
- PHP code for saving the checkbox values.
- Checkbox class for functions.
- List page for listing saved checkboxes.
- Update Page for updating the saved checkboxes.
Lets start with step by step just like the above points.
Create Database with 2 columns Values and Id.
CREATE TABLE IF NOT EXISTS `checkbox` ( `Id` int(3) NOT NULL AUTO_INCREMENT, `Cvalues` varchar(100) NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
You can use the above code for creating the checkbox table with 2 columns for saving the selected data.
Create Html page with multiple checkboxes.
<div class="container"> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> <input type="checkbox" id="Country" name="Country[]" value="India">India<br/> <input type="checkbox" id="Country" name="Country[]" value="USA">USA<br/> <input type="checkbox" id="Country" name="Country[]" value="Australia">Australia<br/> <input type="checkbox" id="Country" name="Country[]" value="Europe">Europe<br/> <input type="checkbox" id="Country" name="Country[]" value="Italy">Italy<br/> <br/><br/> <input type="submit" id="submit" name="submit" value="Submit Values" class="btn btn-primary"> </form> </div> <!-- /container -->
by using the above code you will see some checkbox in the page. One important thing is we have added the [] to the name attribute of the input. [] helps us to create the array of the checkbox for using it in PHP.
PHP code for saving the checkbox values.
<?php if(isset($_POST["submit"])){ $countryarr=$_POST["Country"]; $newvalues= implode(",", $countryarr); include_once './checkboxClass.php'; $checkBoxClass=new checkboxClass(); echo $checkBoxClass->addtoDatabase($newvalues); } ?>
In this we have used a Class and after creating the object of the class we are using the functions of that class. In here we are using the addtoDatabase() function for saving the multiple checkbox values to database. This function will take the values and save it to the database.
public function addtoDatabase($value){ $insert="Insert into checkbox (Cvalues) values ('$value')"; $result=$this->query($insert) or die($this->error); if($result){ return "<h2 class='text-success'>Updated</h2>"; } else { return "<h2 class='text-danger'>Not updated</h2>"; } }
Checkbox class for functions.
Checkbox class will be used for multiple functions which will handle the saving, listing and updating the checkbox values in the database. You can download the whole class from the bottom of the tutorial.
List page for listing saved checkboxes.
Listing page will be used for showing the saved values and providing the area to the user so that he can edit the particular values. For listing the values we are using the table.
<?php include_once './checkboxClass.php'; $checkBoxClass=new checkboxClass(); $list=$checkBoxClass->listCheckbox(""); ?> <div class="container"> <table class="table table-bordered table-responsive"> <thead> <tr> <th>Values</th><th>Action</th> </tr> </thead> <?php if(count($list)){ foreach ($list as $value) { echo "<tr><td>$value[Cvalues]</td><td><a class='btn btn-primary btn-sm' href='edit-checkbox.php?id=$value[Id]'>Edit</a></td></tr>"; } } ?> </table> </div> <!-- /container -->
For list we are using another function from the checkbox class.
public function listCheckbox($query){ $list="select * from checkbox $query"; $result= $this->query($list); $count= $result->num_rows; if($count < 1){}else{ while($row= $result->fetch_array(MYSQLI_BOTH)){ $arr[]= $row; } return $arr; } }
As you can see we have added the checkbox values to the database and listing them over the page for showing the saved values to the user or for further modification.
Update Page for updating the saved checkboxes.
This is the last part of our tutorial. In this part we will just updated the previously saved values and before updating the new values we will see that we will get the values checked according to the saved ones. Like if we have 5 checkboxes and we have checked only 2 than at the time of updating we will get the 2 values already checked when we open the page. It will help us to find out the previous values.
<?php error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE); include_once './checkboxClass.php'; $checkBoxClass=new checkboxClass(); if(isset($_POST["submit"])){ $countryValue= implode(",",$_POST["Country"]); echo $checkBoxClass->updateCheckbox($countryValue, $_GET["id"]); } $list=$checkBoxClass->listCheckbox("Where Id='$_GET[id]'"); $checkboxvalues= explode(",", $list[0]["Cvalues"]); ?> <div class="container"> <table class="table table-bordered table-responsive"> <thead> <tr> <th>Values</th><th>Action</th> </tr> </thead> <?php if(count($list)){ foreach ($list as $value) { echo "<tr><td>$value[Cvalues]</td><td><a class='btn btn-primary btn-sm' href='edit-checkbox.php?id=$value[Id]'>Edit</a></td></tr>"; } } ?> </table> </div>
For updating the values we will use updateCheckbox function.
public function updateCheckbox($value,$id){ $update="update checkbox set Cvalues='$value' Where Id='$id'"; $result=$this->query($update); if($result){ return "Updated"; } else { return "Error"; } }
Hope you like the tutorial and if you have any questions than please comment below. And please subscribe my youtube page.
Thanks for responding. I am only able update one set of check boxes. I can retrieve both sets of checkboxes but the 2nd one does not update when I check options using the checkboxes. I’ve tried added the necessary options but it won’t work.
Thanks!
Hi, Is there a way to make it so you added another set? say one for Countries and then a set of checkboxes for cities. My form has multiple sets of checkboxes. I’ve tried editing your code but I am getting some errors.
What error you are getting.
If possible could you make it do it has 2 sets of checkboxs and add also some other option like a textbox and text area and make them all update on one page?
Thanks!
You need different Name for all the select boxes and other things then you will be able to update all of them at once
thank u bro
How do I fill the check boxes in html form by the data present in the database ?
You can use the loop while doing it. Or just use the jquery to mark them checked