Insert Value From CheckBox In Database (MySQL) In PHP

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

Create Table

2nd.jpg

Create config.php file

Now we will create a config.php file for connecting the database to all PHP files.

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

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

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. Like 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 insertion of multiple data in a MySQL database.

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all