Introduction
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
- <?php
- $con = mysql_connect ("localhost", "root" , "" ) ;
- $ db="MCN";
- if ( ! $con)
- die ( ' Could not connect: ' . mysql_error( ) ) ;
- if (mysql_query ("CREATE DATABASE $db" , $con) )
- echo "Your Database Created Which Name is : $db";
- }
- else
- {
- echo "Error creating database: " . mysql_error( ) ;
- }
- mysql_close ($con) ;
- ?>
- <?php
- $con = mysql_connect ("localhost", "root", "" ) ;
- if ( ! $con)
- die ( ' Could not connect: ' .mysql_error ( ) ) ;
- }
- mysql_select_db ("MCN", $con) ;
- $sql = "CREATE TABLE EMPLOYEE
- (
- Name varchar ( 50) ,
- )" ;
- mysql_query($sql, $con) ;
- mysql_close ($con) ;
- ?>
Now we will create a config.php file for connecting the database to all PHP files.
- <?php
- /* Database Connection */
- $sDbHost = 'localhost' ;
- $sDbName = 'mcn' ;
- $sDbUser = 'root';
- $sDbPud = '';
- $ Conn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd);
- mysql_select_db ($sDbName, $Conn);
- ?>
Now we will create a form.php file by which we will insert a value through checkboxes in the MySQL database.





Abhishek DongrePosted Sep 25, 2017, 11:55 AM
<?php$conn = mysqli_connect("localhost","root","","furniture"); $vehicle= $_POST['vehicle']; if($_POST["Submit"] == "Submit") { for($i=0; $i<sizeof($vehicle);$i++) { $query="INSERT INTO events (status) VALUES ('".$vehicle[$i]."')"; mysqli_query($query) or die (mysqli_error()); } echo "updated"; } ?> <html> <body> <form action="test.php" method="post"> <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br> <input type="submit" value="Submit"> </form> </body> </html> it is showing undefined index vehicle and undefined index submit. please help
Atcharawan BoonvearojkulPosted Apr 11, 2017, 6:09 AM
Thank you so much. this is example as good.because this example code make my project passed so well.
mauro bonucciPosted May 2, 2013, 1:35 PM
If i want to put one more field for example name how do i insert one more variable in a 'for'?
nur hannaneditedPosted Mar 13, 2013, 3:03 PMEdited Mar 13, 2013, 3:04 PM
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";?>
rajesh selvamPosted Dec 19, 2012, 3:46 AM
excellent tutor.. i was done successfully few columns these were wrote here..fullname,sex(radio button),date,address,available course(check box),,etc.thanks
Abhinav AnandPosted Oct 3, 2012, 11:38 AM
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 ?
Timothy JohnstonPosted Aug 26, 2012, 9:27 AM
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?
NasraPosted Aug 21, 2012, 2:12 PM
i want to insert values/input of every textbox that is checked on a form in to a new row of mySQL database with active staus for checked checkboxes. How to do it ??