banner



How To Add Checkbox In Sql Database

  • Updated date Jun 16, 2020
  • 809k
  • v

In this article you volition see how to insert a value from a checkbox in the MySQL database.

Introduction

In the final article, 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 you lot tin insert multiple values in the MySQL database in PHP.

Starting time of all, nosotros 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. dice  ( ' Could not connect: '  .mysql_error ( ) ) ;
  5. }
  6. mysql_select_db ("MCN" , $con ) ;
  7. $sql  = "CREATE Tabular array EMPLOYEE
  8. (
  9. Name varchar ( 50) ,
  10. )" ;
  11. mysql_query($sql , $con ) ;
  12. mysql_close ($con ) ;
  13. ?>

Create a config.php file

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

  1. <?php
  2. $sDbHost  = 'localhost'  ;
  3. $sDbName  = 'mcn'  ;
  4. $sDbUser  = 'root' ;
  5. $sDbPud  = '' ;
  6. $ Conn = mysql_connect ($sDbHost , $sDbUser , $sDbPwd );
  7. mysql_select_db ($sDbName , $Conn );
  8. ?>

Create the form.php file

Now we will create a form.php file past which we will insert a value through checkboxes in the MySQL database.

  1. <html>
  2. <head>
  3. <championship> 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 blazon="checkbox"  proper name= "chkl[ ]"  value= "Priyanka Sachan" > Priyanka Sachan<br />
  13. <br>
  14. <input type="submit"  name= "Submit"  value= "Submit" >
  15. </class>
  16. </body>
  17. </html>

Create checkbox.php file

No, we will create a PHP file in which nosotros include a config.php file and write the insert control 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($query ) or die (mysql_error());
  9. }
  10. echo "Record is inserted" ;
  11. }
  12. ?>

Output

For running the to a higher place 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.east. Vineet Saini, Ravi Sharma, Rahul Dube, Priyanka Sachan. And then nosotros will click on the submit push.

form2.jpg

When we click on the submit button so you will get a message i.e. Record is inserted.

form3.jpg

At present you volition see at that place are four records inserted in the database. Every bit in the following epitome.

last.jpg

Determination

So in this article, you saw how to insert multiple records in a MySQL database through a checkbox. Using this article ane can easily sympathize the insertion of multiple data in a MySQL database.

Some Helpful Resources

  • Insert Value From Radio Button in MySQL in PHP
  • Database Connectivity (Insert and Select) With MySQL in PHP
  • Insert Statement With PHP Script
  • How to Handle Null Values in MYSQL Server
  • Brandish Data in GridView Using MySQL

How To Add Checkbox In Sql Database,

Source: https://www.c-sharpcorner.com/UploadFile/051e29/insert-value-from-checkbox-in-database-mysql-in-php/

Posted by: davisonanall1964.blogspot.com

0 Response to "How To Add Checkbox In Sql Database"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel