Home > DeveloperSection > Forums > How to insert multiple values selected in checkbox in database
pintu nayak
pintu nayak

Total Post:10

Points:70
Posted on    March-27-2012 6:49 AM

 PHP PHP 
Ratings:


 5 Reply(s)
 42677  View(s)
Rate this:
Dear all,

How to insert multiple values selected in checkbox in database.

Thanks


Awadhendra Tiwari

Total Post:126

Points:882
Posted on    March-28-2012 4:51 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.

pintu nayak
pintu nayak

Total Post:10

Points:70
Posted on    March-28-2012 10:20 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

Arun Singh

Total Post:68

Points:476
Posted on    March-29-2012 3:44 AM

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 Mar-29-2012 03:48:26 AM

amy ya
amy ya

Total Post:1

Points:7
Posted on    September-23-2013 1:37 AM

hi, i wanna ask a question..i've got the same problem..help me please..what should i replace 'things' with? my DB column or what...
<?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());
    }
    ?>


Phagu MAhato

Total Post:3

Points:21
Posted on    December-27-2013 6:09 AM

$query= "INSERT INTO individual_skills(Skills_ID,Ind_ID) VALUES (" .$skills[$i]. ",".$_SESSION['Ind_ID'].")";

  for ($i=0; $i<sizeof($skills);$i++) {
 
  $query.= " VALUES (" .$skills[$i]. ",".$_SESSION['Ind_ID'].")";
  }

Don't want to miss updates? Please click the below button!

Follow MindStick