How To Load Data In Sql And Show It In The Form Of Checkbox
- Updated date Jun 16, 2020
- 802.4k
- v
In this article you volition encounter how to insert a value from a checkbox in the MySQL database.
Introduction
In the last commodity, you saw how to insert a value from a radio button in the MySQL database in PHP. Now in this article, you volition see how to insert a value from a checkbox in the MySQL database. Using a checkbox yous 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 )
- dice ( ' Could non connect: ' .mysql_error ( ) ) ;
- }
- mysql_select_db ("MCN" , $con ) ;
- $sql = "CREATE TABLE EMPLOYEE
- (
- Proper noun varchar ( 50) ,
- )" ;
- mysql_query($sql , $con ) ;
- mysql_close ($con ) ;
- ?>
Create a config.php file
Now nosotros volition 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
At present 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" proper name= "chkl[ ]" value= "Vineet Saini" >Vineet Saini<br />
- <input blazon="checkbox" proper 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 blazon="checkbox" proper noun= "chkl[ ]" value= "Priyanka Sachan" > Priyanka Sachan<br />
- <br>
- <input type="submit" proper noun= "Submit" value= "Submit" >
- </form>
- </body>
- </html>
Create checkbox.php file
No, nosotros 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 (proper name) VALUES ('" . $checkboxl [ $i ]. "')" ;
- mysql_query($query ) or die (mysql_error());
- }
- repeat "Record is inserted" ;
- }
- ?>
Output
For running the above lawmaking we volition write in the web browser "
http://localhost/foldername/class.php".
Now we select any checkbox. Suppose we select four checkboxes i.eastward. Vineet Saini, Ravi Sharma, Rahul Dube, Priyanka Sachan. Then we will click on the submit button.
When we click on the submit button then yous will become a message i.east. Tape is inserted.
Now you volition encounter there are four records inserted in the database. As in the following epitome.
Determination
And then in this article, you saw how to insert multiple records in a MySQL database through a checkbox. Using this article one can easily sympathize the insertion of multiple data in a MySQL database.
Some Helpful Resources
- Insert Value From Radio Push button in MySQL in PHP
- Database Connectivity (Insert and Select) With MySQL in PHP
- Insert Statement With PHP Script
- How to Handle Zippo Values in MYSQL Server
- Brandish Data in GridView Using MySQL
How To Load Data In Sql And Show It In The Form Of Checkbox,
Source: https://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/
Posted by: monarrezyousses.blogspot.com
0 Response to "How To Load Data In Sql And Show It In The Form Of Checkbox"
Post a Comment