ARTICLE

Insert Value From CheckBox in Database (MySQL) in PHP

Posted by Vineet Kumar Saini Articles | PHP April 15, 2012
In this article you will see how to insert a value from a checkbox in the MySQL database.
Reader Level:

In the last article you saw how to insert a value from a radio button in the MySQL database in PHP. Now in this article you will see how to insert a value from a checkbox in the MySQL database. Using a checkbox you can insert multiple values in the MySQL database in PHP.

First of all we will create a database and a table in MySQL.

 

Create Database

 

1st.jpg

 

Create Table

 

2nd.jpg

Create config.php file

 

Now we will create a config.php file for connecting the database to all PHP files.

 

img3.jpg

Create the form.php file

 

Now we will create a form.php file by which we will insert a value through checkboxes in the MySQL database.

 

img2.jpg

 

Create checkbox.php file

 

No we will create a PHP file in which we include a config.php file and write the insert command for inserting the data in the database.

 

img1.jpg

 

Output

 

For running the above code we will write in the web browser "http://localhost/foldername/form.php" .

 

form.jpg

 

Now we select any checkbox. Suppose we select four checkboxes i.e. Vineet Saini, Ravi Sharma, Rahul Dube, Priyanka Sachan. Then we will click on the submit button.

 

form2.jpg

 

When we click on the submit button then you will get a message i.e. Record is inserted.

 

form3.jpg

 

Now you will see there are four records inserted in the database. Like as in the following image.

 

last.jpg

 

Conclusion

 

So in this article you saw how to insert multiple records in a MySQL database through a checkbox. Using this article one can easily understand insertion of multiple data in a MySQL database.


Some Helpful Resources

Login to add your contents and source code to this article
post comment
     

If i want to put one more field for example name how do i insert one more variable in a 'for'?

Posted by mauro bonucci May 02, 2013

can u..helped solve this up?..pls....for mylast final project..."Notice: Undefined variable: i in C:\xampp\htdocs\cuba\checkbox.php on line 6 Column count doesn't match value count at row 1"============================ checkbox.php <?php include ("connectdatabase.php"); $chk = $_POST['chk1']; if($_POST["submit"]=="submit")$query="INSERT INTO senarai_stud (kod_kursus,nama_kursus,class) VALUES ('".$chk[$i]."')";mysql_query($query) or die(mysql_error());echo "Maklumat anda telah dihantar";?>

Posted by nur hannan Mar 13, 2013

excellent tutor.. i was done successfully few columns these were wrote here..fullname,sex(radio button),date,address,available course(check box),,etc.thanks

Posted by rajesh selvam Dec 19, 2012

Suppose i had 2 text field and another field called hobby with 4 options as checkbox, of which we could select any one! How would we then add the vales to the database? Would it be the same as the radio button exapmple, with only checkbox type in place of radio? Or would there be more difference ?

Posted by Abhinav Anand Oct 03, 2012

This is great. How could I include 2nd and 3rd columns into the POST? For example, if there was a column for age and city, how would I include these into the post?

Posted by Timothy Johnston Aug 26, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter