PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

In this post on Inserting Information from Multiple CheckBox Selection in a Database Table in PHP, I will demonstrate how to insert rows of data in a database table using a selection of multiple checkboxes in PHP.

Example of Multiple CheckBox selection

In order to demonstrate that, first of all, we need to create a database table. After that, we create a Form in HTML with certain checkboxes and a Submit button. Finally, we retrieve the form data and execute insert queries a number of times according to the number of checkboxes selected.

Database Table Creation

As can be seen below, the Create Table command creates a table in the current database with the name event_registration. Further, the table has two fields – registration_id, and event_name. Additionally, the registration_id is an auto-increment field. Therefore, in the insert commands, we need to provide values of only the event_name field.

create TABLE event_registration(registration_id int PRIMARY key AUTO_INCREMENT, event_name varchar(20))

Creating the Form

To begin with, we create a PHP file called multiplecheckboxes.php. At first, we create the required styles and then we create an HTML form. Besides the checkboxes, the form has a Submit button. Also, the <form> tag shows that when the form is submitted, another PHP file called SaveMyChoices.php will execute.

Since we need to retrieve the information about multiple checkbox selection, the name attribute in the <input> tag for checkboxes contains an array called eventname[]. Hence, we can obtain the values contained in this array as a POST variable.

multiplecheckboxes.php

<style>
    
.container1 {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}

/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .col-25, .col-75, input[type=submit] {
    width: 100%;
    margin-top: 0;
  }
}
</style>
<div class="container container1 mt-10">
  <div class="container">
    <form action="SaveMyChoices.php" method="POST">
      <h3> What the event in which you wamnt to participate?</h3>
        <p>
          <ol>
  <input type="checkbox" id="c1" name="eventname[]" value="BusinessPlan">
  <label for="bp">BUSINESS PLAN</label><br>
  <input type="checkbox" id="c2" name="eventname[]" value="BusinessQuiz">
  <label for="bq">BUSINESS QUIZ</label><br>
  <input type="checkbox" id="c3" name="eventname[]" value="AdMad">
  <label for="am">AD MAD</label><br>
  <input type="checkbox" id="c4" name="eventname[]" value="AdSelfie">
  <label for="as">AD-SELFIE</label><br>
  <input type="checkbox" id="c5" name="eventname[]" value="Innovatz">
  <label for="in">INNOVATZ</label><br>
  <input type="checkbox" id="c6" name="eventname[]" value="MasterMinds">
  <label for="mm">MASTER MINDS</label><br>
  <input type="checkbox" id="c7" name="eventname[]" value="Codesense">
  <label for="cs">CODESENSE</label><br>
   <input type="checkbox" id="c8" name="eventname[]" value="Fuzon">
  <label for="fu">FUZON</label><br>
  <input type="checkbox" id="c9" name="eventname[]" value="Gaming">
  <label for="ga">GAMING</label><br>
  <input type="checkbox" id="c10" name="eventname[]" value="TreasureHunt">
  <label for="th">TREASURE HUNT</label>
        </ol>
    </p>
    
    <br/><br/>
    
   <center>   <input type="submit" value="Submit"> </center>

  </form>
        
</div>
</div>

Creating Database Connection

As can be seen, we need to create a database connection using PDO. The following code shows how to create a connection with a MySQL database using PDO. For this purpose, we create a PHP file known as db.php in a folder called db.

db.php

<?php
$dsn='mysql:host=localhost; dbname=your_database_name';
$username='your_username';
$password='your_password';
$con1=new PDO($dsn, $username, $password);


?>

Collecting Form Data and Executing the Query

Finally, we can retrieve the information about Multiple CheckBox Selection in an array and using the foreach loop execute the insert command specified number of times.

SaveMyChoices.php

<?php
    require_once('db/db.php');
    
?>
<?php
$arr=$_POST['eventname'];
foreach($arr as $a)
{
    $sql="insert into event_registration(event_name) values(:ev)";
    $stmt=$con1->prepare($sql);
    $stmt->bindValue(':ev', $a);

    $ex=$stmt->execute();

    if($ex)
    {
        $res= 'Thank You for Registering!';
    }
    else
   {
     $res= 'Some Error has Occurred while inserting data in the table!';
   }
}
echo $res;
?>

Output

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
Multiple CheckBox Selection

Submitting the Form

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

Database Table after Form Submission

Database Table after Form Submission
Database Table after Form Submission

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *