.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Insert Value from Checkbox in MySql Database in PHP

Posted By:satyapriyanayak       Posted Date: February 21, 2014    Points: 40    Category: Collections    URL: http://www.dotnetspark.com  

In this Code Snippets 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";
}
?>


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend