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) ;
- ?>
Create Table
- <?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) ;
- ?>
Create a config.php file
Now we will create a config.php file for connecting the database to all PHP files.
- <?php
-
- $sDbHost = 'localhost' ;
- $sDbName = 'mcn' ;
- $sDbUser = 'root';
- $sDbPud = '';
- $ Conn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd);
- mysql_select_db ($sDbName, $Conn);
- ?>
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.
- <html>
- <head>
- <title> PHP Form<</title>
- </head>
- <body bgcolor="pink">
- <h3>Insert Value From Checkboxes</h3>
- <form action="checkbox . php" method="post">
- <input type="checkbox" name="chkl[ ]" value="Vineet Saini">Vineet Saini<br />
- <input type="checkbox" name="chkl[ ]" value="Ravi Sharma">Ravi Sharma<br />
- <input type="checkbox" name="chkl[ ]" value="Rahul Dube">Rahul Dube<br />
- <input type="checkbox" name="chkl[ ]" value="Rajesh Verma">Rajesh Verma<br />
- <input type="checkbox" name="chkl[ ]" value="Priyanka Sachan"> Priyanka Sachan<br />
- <br>
- <input type="submit" name="Submit" value="Submit">
- </form>
- </body>
- </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.
- <php
- include ("config . php");
- $checkbox1 = $_POST['chkl'] ;
- if ($_POST["Submit" ]=="Submit")
- {
- for ($i=0; $i<sizeof ($checkbox1);$i++) {
- $query="INSERT INTO employee (name) VALUES ('".$checkboxl[$i]. "')";
- mysql_query($query) or die(mysql_error());
- }
- echo "Record is inserted";
- }
- ?>
Output
For running the above code we will write in the web browser "http://localhost/foldername/form.php".
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.
When we click on the submit button then you will get a message i.e. Record is inserted.
Now you will see there are four records inserted in the database. As in the following image.
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