Insert Multiple Checkbox Value In Database Using PHP

By : PHPErrorCode August 7, 2017 761 Views php

Now Today in this post you will see how to insert a value from one or more checkbox in the MySQL database. Using a checkbox you can insert multiple values in the MySQL database in PHP. In this demo, we implement insert multiple checkbox values in a database in PHP. There are certain steps we need to follow as explained below.

Using the form in more than one checkbox form, it is difficult to add data to the data bus, through which this post allows you to easily add more than one checkbox to the databse, which we can add to the step by step.

Step 1: First of all we will create a database and a table in MySQL.

  1. Open phpmyadmin
  2. Create database as "tutorial"
  3. create table and name it as "technology"
CREATE TABLE `technology` (
  `id` int(11) NOT NULL,
  `technology_name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2: Connect a database using MySQL:for config.php

In this step we are going to connect the database with our project.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tutorial";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // Check connection
  if ($conn->connect_error) 
  {
      die("Connection failed: " . $conn->connect_error);
  }

Step 3: Front end code, Make HTML Structure for list of checkbox in technology.php

<?php 
require_once('config.php');
session_start(); 
?>
<html>  
<body>  
    <?php 
      if (isset($_SESSION['msg'])) {
         echo $_SESSION['msg'];
      } 
    ?>
    <form action="add_technology.php" method="post">   
      <table border="1">  
         <tr>  
            <td colspan="2">Select Technolgy:</td>  
         </tr>  
         <tr>  
            <td>PHP</td>  
            <td><input type="checkbox" name="technology[]" value="PHP"></td>  
         </tr>  
         <tr>  
            <td>Java</td>  
            <td><input type="checkbox" name="technology[]" value="Java"></td>  
         </tr>  
         <tr>  
            <td>.net</td>  
            <td><input type="checkbox" name="technology[]" value="net"></td>  
         </tr>  
         <tr>  
            <td>Androiad</td>  
            <td><input type="checkbox" name="technology[]" value="Androiad"></td>  
         </tr>  
         <tr>  
            <td colspan="2" align="center"><input type="submit" value="submit" name="submit"></td>  
         </tr>  
      </table>   
    </form>
    <table border="1" width="100%">
      <tr>
         <th>Id</th>
         <th>Technology Name</th>
         <th>Action</th>
      </tr>
      <?php
            $result = $conn->query("SELECT * FROM technology");
            if(!$result->num_rows > 0){ echo 'No Data Faund'; }
            while($row = $result->fetch_assoc())
            {
         ?>
            <tr>
               <td><?php echo $row['id']; ?></td>
               <td><?php echo $row['technology_name']; ?></td>
               <td><a href="show.php?id=<?php echo $row['id']; ?>">show</a></td>
            </tr>
      <?php } ?>
    </table>
</body>  
</html>

Step 4: inserted recored on the submited form

Create A New File add_technology.php And Place The Code Below Into It.

<?php
require_once('config.php');
session_start();

$tec = $_POST['technology'];

    if(!empty($tec)) 
    {
	$technology="";

	for ($i=0; $i < count($tec); $i++) 
	{ 
	    if($i == count($tec)-1)
	    {
		$technology .=$tec[$i].'.';
	    }
	    else
	    {
		$technology .=$tec[$i].',';
	    }
	}

	$data=$conn->prepare("INSERT INTO technology (technology_name) VALUES(?)");

	// Bind the variables to the parameter as strings. 
	$data->bind_param("s", $technology);

	// Execute the prepared statement.
	$data->execute();

	// Close the prepared statement.
	$data->close();

	$_SESSION['msg']="Add Data Seccess...";
    }
    else
    {
	$_SESSION['msg']="Data Empty...";
    }

    header("location: index.php");
?>

Step 5: show single recored

Create A New File show.php And Place The Code Below Into It.

<?php 
require_once('config.php');
session_start(); 

if($result = $conn->prepare("SELECT * FROM technology where id=?") or die($conn->connect_errno))
{
    // Bind the variables to the parameter as strings.
    $result->bind_param('s', $_GET['id']);

    // Execute the prepared statement.
    $result->execute();

    // Gets a result set from a prepared statement.
    $result = $result->get_result();

    // Fetch the table data.
    $row = $result->fetch_assoc();

    $technology = explode(',', $row['technology_name']);
}
?>
<html>  
<body>  
   <?php 
      if (isset($_SESSION['msg'])) {
         echo $_SESSION['msg'];
      } 
   ?>
   <form action="update_technology.php" method="post">   
      <table border="1">  
         <tr>  
            <td colspan="2">Select Technolgy:</td>  
         </tr>  
         <tr>  
            <td>PHP</td>  
            <td>
                <input type="checkbox" name="technology[]" value="PHP" <?php foreach($technology as $key => $value){ if($value == "PHP") { echo "checked";} } ?>
                >
            </td>  
         </tr>  
         <tr>  
            <td>Java</td>  
            <td><input type="checkbox" name="technology[]" value="Java" <?php foreach($technology as $key => $value){ if($value == "Java") { echo "checked";} } ?>
            >
        </td>  
         </tr>  
         <tr>  
            <td>.net</td>  
            <td><input type="checkbox" name="technology[]" value="net" <?php foreach($technology as $key => $value){ if($value == "net") { echo "checked";} } ?>
            >
        </td>  
         </tr>  
         <tr>  
            <td>Androiad</td>  
            <td><input type="checkbox" name="technology[]" value="Androiad" <?php foreach($technology as $key => $value){ if($value == "Androiad") { echo "checked";} } ?>
            >
        </td>  
         </tr>  
         <tr>  
            <td colspan="2" align="center">
                <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                <input type="submit" value="Update" name="submit">
            </td>  
         </tr>  
      </table>   
   </form>
</body>  
</html>

Step 6: Updaet recored on the submited form

Create A New File update_technology.php And Place The Code Below Into It.

<?php
require_once('config.php');
session_start();

$tec = $_POST['technology'];

  if(!empty($tec)) 
  {
    $technology="";

    for ($i=0; $i < count($tec); $i++) 
    { 
      if($i == count($tec)-1)
      {
        $technology .=$tec[$i].'';
      }
      else
      {
        $technology .=$tec[$i].',';
      }
    }

    $data=$conn->prepare("UPDATE technology SET technology_name=? WHERE id=?");

    // Bind the variables to the parameter as strings. 
    $data->bind_param('si', $technology, $_POST['id']);

    // Execute the prepared statement.
    $data->execute();

    // Close the prepared statement.
    $data->close();

    $_SESSION['msg']="Update Data Seccess...";
  }
  else
  {
    $_SESSION['msg']="Data Empty...";
  }

  header("location: show.php?id=".$_POST['id']);
?>