Sponsored By
MindStick SurveyManager
Advertise with Us
Advertisement
Advertise with Us
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
   

Home >> Other Programming >> PHP >> How to insert multiple values selected in checkbox in database
Author Post

pintu nayak


Total Post: 10
Member Since: 11/15/2011
Points: 54
How to insert multiple values selected in checkbox in database
Posted On: 3/27/2012 6:49:27 AM
Dear all,

How to insert multiple values selected in checkbox in database.

Thanks
Author Post

Awadhendra Tiwari


Total Post: 126
Member Since: 1/19/2011
Points: 893
Re: How to insert multiple values selected in checkbox in database
Posted On: 3/28/2012 4:51:00 AM
Hi Pintu,
If you are talking about group of checkbox with same name and you want to insert values of those checkbox which is checked, then you have to write following script to retrive values of those checkbox.

      <?php
    $checkBox = $_POST['things'];
    for($i=0; $i<sizeof($checkBox); $i++){
    $query = "INSERT INTO table(things) values('".$checkBox[$i]."')";
    mysql_query($query) or die(mysql_error());
    }
    ?>

If this is not correct answer then please clarify your question that what you want to do with multiple checkbox in php.
Author Post

pintu nayak


Total Post: 10
Member Since: 11/15/2011
Points: 54
Re: How to insert multiple values selected in checkbox in database
Posted On: 3/28/2012 10:20:18 AM
Hello friend,

I want to insert the values of the checkbox when checked and then click the button.After clicking the button all the checked checkbox values will be inserted into the database.

My code

form.php

<html>
<head>
<title> PHP Form</title>
</head>
<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="chk1" value="a">a<br />
<input type="checkbox" name="chk1" value="b">b<br />
<input type="checkbox" name="chk1" value="c">c<br />
<input type="checkbox" name="chk1" value="d">d<br />
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


checkbox.php

<?php
session_start();
include("config.php");
$checkbox1 = $_POST['chk1'];

if($_POST["Submit"]=="Submit")
{

for ($i=0; $i<sizeof($checkbox1);$i++) {
$query="INSERT INTO check (type) VALUES ('".$checkbox1[$i]."')";
mysql_query($query) or die ('Error updating database');
echo "Record is inserted.";

}
}
?>


config.php

<?php

 $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());

?>


Please help me iam new to php.

Thanks
Author Post

Arun Singh


Total Post: 49
Member Since: 7/1/2011
Points: 338
Re: How to insert multiple values selected in checkbox in database
Posted On: 3/29/2012 3:44:23 AM
Report Abuse Answer
Hi Pintu Nayak,
In this code, you've done a little mistake in 'form.php' file. Replace your code with following code:
'form.php' Code:
<html>
<head>
<title> PHP Form</title>
</head>
<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="chk1[]" value="a">a<br />
<input type="checkbox" name="chk1[]" value="b">b<br />
<input type="checkbox" name="chk1[]" value="c">c<br />
<input type="checkbox" name="chk1[]" value="d">d<br />
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

Please marked as answer if this code will solve your problem.
Modified On: 3/29/2012 3:48:26 AM
Report Abuse Form
Reason:    
 
Total Online Users: 2604
Advertisement
MindStick DataConverter
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.