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](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/1st.jpg)
Create Table
![2nd.jpg](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/2nd.jpg)
Create config.php file
Now we will create a config.php file for connecting the database to all PHP files.
![img3.jpg](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/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](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/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](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/img1.jpg)
Output
For running the above code we will write in the web browser "http://localhost/foldername/form.php" .
![form.jpg](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/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](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/form2.jpg)
When we click on the submit button then you will get a message i.e. Record is inserted.
![form3.jpg](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/form3.jpg)
Now you will see there are four records inserted in the database. Like as in the following image.
![last.jpg](http://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/Images/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.
No comments:
Post a Comment