Insert Value From CheckBox In Database (MySQL) In PHP

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
  1. <?php    
  2. $con = mysql_connect ("localhost""root" , "" ) ;    
  3. $ db="MCN";    
  4. if ( ! $con)    
  5. die ( ' Could not connect: ' . mysql_error( ) ) ;    
  6. if (mysql_query ("CREATE DATABASE $db" , $con) )    
  7. echo "Your Database Created Which Name is : $db";    
  8. }    
  9. else    
  10. {    
  11. echo "Error creating database: " . mysql_error( ) ;    
  12. }    
  13. mysql_close ($con) ;    
  14. ?>    
Create Table
  1. <?php  
  2. $con = mysql_connect ("localhost""root""" ) ;  
  3. if ( ! $con)  
  4. die ( ' Could not connect: ' .mysql_error ( ) ) ;  
  5. }  
  6. mysql_select_db ("MCN"$con) ;  
  7. $sql = "CREATE TABLE EMPLOYEE  
  8. (  
  9. Name varchar ( 50) ,  
  10. )" ;  
  11. mysql_query($sql$con) ;  
  12. mysql_close ($con) ;  
  13. ?>  
Create a config.php file
 
Now we will create a config.php file for connecting the database to all PHP files.
  1. <?php  
  2. /* Database Connection */  
  3. $sDbHost = 'localhost' ;  
  4. $sDbName = 'mcn' ;  
  5. $sDbUser = 'root';  
  6. $sDbPud = '';  
  7. $ Conn = mysql_connect ($sDbHost$sDbUser$sDbPwd);  
  8. mysql_select_db ($sDbName$Conn);  
  9. ?>  
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.
  1. <html>  
  2. <head>  
  3. <title> PHP Form<</title>  
  4. </head>  
  5. <body bgcolor="pink">  
  6. <h3>Insert Value From Checkboxes</h3>  
  7. <form action="checkbox . php" method="post">  
  8. <input type="checkbox" name="chkl[ ]" value="Vineet Saini">Vineet Saini<br />  
  9. <input type="checkbox" name="chkl[ ]" value="Ravi Sharma">Ravi Sharma<br />  
  10. <input type="checkbox" name="chkl[ ]" value="Rahul Dube">Rahul Dube<br />  
  11. <input type="checkbox" name="chkl[ ]" value="Rajesh Verma">Rajesh Verma<br />  
  12. <input type="checkbox" name="chkl[ ]" value="Priyanka Sachan"> Priyanka Sachan<br />  
  13. <br>  
  14. <input type="submit" name="Submit" value="Submit">  
  15. </form>  
  16. </body>  
  17. </html>  
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.
  1. <php  
  2. include ("config . php");  
  3. $checkbox1 = $_POST['chkl'] ;  
  4. if ($_POST["Submit" ]=="Submit")  
  5. {  
  6. for ($i=0; $i<sizeof ($checkbox1);$i++) {  
  7. $query="INSERT INTO employee (name) VALUES ('".$checkboxl[$i]. "')";  
  8. mysql_query($queryor die(mysql_error());  
  9. }  
  10. echo "Record is inserted";  
  11. }  
  12. ?>  
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. 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 the insertion of multiple data in a MySQL database.
 
Some Helpful Resources


Similar Articles