PHP Insert checkbox selected value in MySQL database

How to send/store only single checkbox item clicked value to database using php.

In this tutorial we are creating simple checkbox to store value inside MySQL database. Check box is used to get single type of multiple values choice from user in web applications. So here is the complete step by step tutorial for PHP Insert checkbox selected value in MySQL database.

android-project-download-code-button

PHP Insert checkbox selected value in MySQL database.

Code for checkbox-single-value-DB.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Insert checkbox selected value in MySQL database</title>
</head>

<body>

<h2>PHP Insert checkbox selected value in MySQL database</h2>
 <form method="post" action="">
 <input type="checkbox" name="check[]" value="Computer"><label>COMPUTER</label><br/>
 <input type="checkbox" name="check[]" value="Laptop"><label>LAPTOP</label><br/>
 <input type="checkbox" name="check[]" value="Mobile"><label>MOBILE</label><br/>
 <input type="submit" name="submit" Value="submit">
 </form>
 
<?php

include 'dbconfig.php';

if (isset($_POST['submit']))
{
 $chkbox = $_POST['check'];
 
 $i = 0;
 
 While($i<sizeof($chkbox))
 {
 
 $query = "INSERT INTO demo_table_4 (CheckboxValue) VALUES ('".$chkbox[$i]."')";
 
 mysql_query($query) or die(mysql_error());
 
 $i++;
 }
 echo "Successfully Submitted.";
 }

?>
</body>
</html>

Code for dbconfig.php layout file.

 <?php

//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);



?>

Screenshot:

PHP Insert checkbox selected value in MySQL database

Click here to download PHP Insert checkbox selected value in MySQL database project source code file with SQL database file included.

Leave a Reply

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