---
title: "How to insert multiple values selected in checkbox in database"  
description: "How to insert multiple values selected in checkbox in database"  
author: "pintu nayak"  
published: 2012-03-27  
updated: 2013-12-27  
canonical: https://www.mindstick.com/forum/399/how-to-insert-multiple-values-selected-in-checkbox-in-database  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# How to insert multiple values selected in checkbox in database

Dear all,\
\
How to [insert](https://www.mindstick.com/blog/173/executing-insert-delete-or-update-query-in-sqlserver-using-ado-dot-net) [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [values selected in checkbox](https://www.mindstick.com/forum/33423/insert-different-type-multiple-values-selected-in-checkbox-in-database) in database.\
\
Thanks\

## Replies

### Reply by Phagu MAhato

```
$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'].")";  }
```

### Reply by amy ya

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());    }    ?>
```

\

### Reply by Anonymous User

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. \

### Reply by pintu nayak

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**\
\

```
<?phpsession_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\

### Reply by Anonymous User

Hi Pintu,\
If you are talking about group of [checkbox](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) with same name and you want to insert [values](https://www.mindstick.com/forum/327/sum-textbox-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.\


---

Original Source: https://www.mindstick.com/forum/399/how-to-insert-multiple-values-selected-in-checkbox-in-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
