Insert Multiple Checkbox Value In Database Using PHP

Insert Multiple Checkbox Value In Database Using PHP

In this Post We Will Explain About is Insert Multiple Checkbox Value In Database Using PHP With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to insert multiple checkbox value in a single column of MySQL Example

In this post we will show you Best way to implement How To Insert Multiple Check Box Values Into MySQL, hear for Insert Multiple Checkbox Value in Database Using PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Step 1: create a database and a table using MySQL.

First of all we will create a database and a table in MySQL.
1.First of all Open phpmyadmin
2.Make database as “live24u”
3.simple new create table and name it as “programingLang”

CREATE TABLE `programingLang` (
  `id` int(11) NOT NULL,
  `lan_name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2: Connect a simple database using MySQL

In this step we are going to connect the database with our project: :for db_connection.php.

Also Read :   AngularJS Contact Form Send Email with Attachment using PHP

$sever_hostname = "hostname";
$username = "Your_username";
$password = "yourPassword";
$database_name = "live24u";

  // Make a database connection
  $db_con = new mysqli($sever_hostname, $username, $password, $database_name);

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

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

<?php 
require_once('db_connection.php');
session_start(); 
?>
<html>  
<body>  
    <?php 
      if (isset($_SESSION['res_message'])) {
         echo $_SESSION['res_message'];
      } 
    ?>
    <form action="add_programinglang.php" method="post">   
      <table border="1">  
         <tr>  
            <td colspan="2">Select Programming Lang:</td>  
         </tr>  
         <tr>  
            <td>PHP</td>  
            <td><input type="checkbox" name="programingLang[]" value="PHP"></td>  
         </tr>  
         <tr>  
            <td>AngularJs</td>  
            <td><input type="checkbox" name="programingLang[]" value="AngularJs"></td>  
         </tr>  
         <tr>  
            <td>.ASPNet</td>  
            <td><input type="checkbox" name="programingLang[]" value="ASPNet"></td>  
         </tr>  
         <tr>  
            <td>VueJS</td>  
            <td><input type="checkbox" name="programingLang[]" value="VueJS"></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>Programming Name</th>
         <th>Action</th>
      </tr>
      <?php
            $result = $db_con->query("SELECT * FROM programingLang");
            if(!$result->num_rows > 0){ echo 'No select any Data Faund'; }
            while($row = $result->fetch_assoc())
            {
         ?>
            <tr>
               <td><?php echo $row['id']; ?></td>
               <td><?php echo $row['lan_name']; ?></td>
               <td><a href="dashboard.php?id=<?php echo $row['id']; ?>">show</a></td>
            </tr>
      <?php } ?>
    </table>
</body>  
</html>

Step 4: inserted recored on the submited form

Make A New File add_programinglang.php And Place The source Code Below Into It.

Also Read :   Angular4 Send Email using PHP - Email sending with Angular4

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

$lang = $_POST['programingLang'];

    if(!empty($lang)) 
    {
	$programingLang="";

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

	$sqldata=$db_con->prepare("INSERT INTO programingLang (lan_name) VALUES(?)");
	$sqldata->bind_param("s", $programingLang);
	$sqldata->execute();
	$sqldata->close();

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

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

Step 5: Display single recored

Make A New File dashboard.php And Place The Source Code Below Into It.

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

if($result = $db_con->prepare("SELECT * FROM programingLang where id=?") or die($db_con->connect_errno))
{
    $result->bind_param('s', $_GET['id']);
    $result->execute();
    $result = $result->get_result();
    $row = $result->fetch_assoc();
    $programingLang = explode(',', $row['lan_name']);
}
?>
<html>  
<body>  
   <?php 
      if (isset($_SESSION['res_message'])) {
         echo $_SESSION['res_message'];
      } 
   ?>
   <form action="update_programminglang.php" method="post">   
      <table border="1">  
         <tr>  
            <td colspan="2">Select Programming Lang:</td>  
         </tr>  
         <tr>  
            <td>PHP</td>  
            <td>
                <input type="checkbox" name="programingLang[]" value="PHP" <?php foreach($programingLang as $key => $value){ if($value == "PHP") { echo "checked";} } ?>
                >
            </td>  
         </tr>  
         <tr>  
            <td>AngularJs</td>  
            <td><input type="checkbox" name="programingLang[]" value="AngularJs" <?php foreach($programingLang as $key => $value){ if($value == "AngularJs") { echo "checked";} } ?>
            >
        </td>  
         </tr>  
         <tr>  
            <td>.ASPNet</td>  
            <td><input type="checkbox" name="programingLang[]" value="ASPNet" <?php foreach($programingLang as $key => $value){ if($value == "ASPNet") { echo "checked";} } ?>
            >
        </td>  
         </tr>  
         <tr>  
            <td>VueJS</td>  
            <td><input type="checkbox" name="programingLang[]" value="VueJS" <?php foreach($programingLang as $key => $value){ if($value == "VueJS") { 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

Make A New File update_programminglang.php And Place The source Code Below Into It.

Also Read :   Send Windows Push Notification Services using PHP

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

$lang = $_POST['programingLang'];

  if(!empty($lang)) 
  {
    $programingLang="";

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

    $sqldata=$db_con->prepare("UPDATE programingLang SET lan_name=? WHERE id=?");
    $sqldata->bind_param('si', $programingLang, $_POST['id']);
    $sqldata->execute();
    $sqldata->close();

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

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

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is how to insert single checkbox value in database using php And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.