In this blog we will know how to Insert Value from Checkbox
in MySql Database in PHP.
Table structure
-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 29, 2012 at 05:17 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee`
--
CREATE TABLE `employee` (
`name` varchar(255)
NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `employee`
--
config.php
<?php
/* Database Connection */
$sDbHost =
'localhost';
$sDbName = 'test';
$sDbUser = 'root';
$sDbPwd = '';
$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die
('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select
database. ' . mysql_error());
?>
form.php
<html>
<head>
<title> PHP Form</title>
</head>
<body>
<form action="checkbox.php"
method="post">
<input type="checkbox" name="chk1[]"
value="Raj">Raj<br />
<input type="checkbox" name="chk1[]"
value="Ravi">Ravi<br />
<input type="checkbox" name="chk1[]"
value="Rahul">Rahul<br />
<input type="checkbox" name="chk1[]"
value="Rajesh">Rajesh<br />
<br>
<br>
<input type="submit" name="Submit"
value="Submit">
</form>
</body>
</html>
checkbox.php
<?php
include("config.php");
$checkbox1 = $_POST['chk1'];
if($_POST["Submit"]=="Submit")
{
for ($i=0; $i<sizeof($checkbox1);$i++) {
$query="INSERT INTO employee (name) VALUES
('".$checkbox1[$i]."')";
mysql_query($query) or die(mysql_error());
}
echo "Record is inserted";
}
?>
Thanks for reading