# MySQL, PHP: sending multiple checkbox values

**URL:** <https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609>\
**Category:** programming\
**Created:** [July 6, 2004, 3:24am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609 "2004-07-06T03:24:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carixpig](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/carixpig/32/304_2.png) [@Carixpig](https://forum.kirupa.com/u/Carixpig)\
**Post date:** [July 6, 2004, 3:24am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/1 "2004-07-06T03:24:33Z")

</div>

Hi all,

I can’t seem to work out how to insert multiple checkbox values into a mysql database with php.

As you know, check boxes are for selecting multiple options in the same group. I have a form with three checkboxes, but when I submit the form, only the bottom-most checked box value is inserted into the database.

How do I get them all the checked options in there?

Thanks,  
Carixpig

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 4:59am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/2 "2004-07-06T04:59:10Z")

</div>

show us your code and we can go from there.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 6:39am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/3 "2004-07-06T06:39:06Z")

</div>

This is the check box bit within the form:

```auto

<input type="checkBox" name="entry_type" value="homepage" id="homepage" checked>
          Homepage Announcement<br>
          <input type="checkBox" name="entry_type" value="investor" id="investor">
          Investor Announcement<br>
          <input type="checkBox" name="entry_type" value="customer" id="customer">

```

This is the php page:

```php

<html>
<head>
<title>Your news entry has been inserted</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<?
require 'include.php';

$entry_date = $_POST["entry_date"]; 
$entry_title = $_POST["entry_title"]; 
$entry_summary = $_POST["entry_summary"]; 
$entry_body = $_POST["entry_body"];
$entry_type = $_POST["entry_type"]; 

$cid = mysql_connect($host,$username,$password);
mysql_select_db($database); 
if (!$cid) { print "ERROR: " . mysql_error() . "
"; }

if ($_SERVER['REQUEST_METHOD'] == "POST") {

$sql = " INSERT INTO itl_news";
$sql .= " (entry_date, entry_title, entry_summary, entry_body, entry_type) VALUES ";
$sql .= " ('$entry_date', '$entry_title','$entry_summary','$entry_body', '$entry_type') ";
$result = mysql_query($sql, $cid);
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); } 

print "New Entry Added for <b>$entry_date</b> ($entry_type announcement).";

mysql_close();

}
?>
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 2:56pm UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/4 "2004-07-06T14:56:45Z")

</div>

your best shot is to use \<input name=“entry\_type[]”\>, i.e. to use an array.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 3:26pm UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/5 "2004-07-06T15:26:00Z")

</div>

I suggest you review the data you are recieving prior to using it.  
Use print\_r($\_POST) to review all the data you are receiving and then decide how you are going to process the input.

[color=black]If I recall correctly you will be getting multiple $\_POST[“entry\_type”] data pairs and you need to process all of them. You may want to concantenate them with $entry\_type .= $\_POST[“entry\_type”]; in a for loop or you may want to store each individually. Depends upon desire to do a look on the data and ease of displaying it.[/color]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2004, 3:44pm UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/6 "2004-07-06T15:44:09Z")

</div>

> [@OrionLAB](#):
>
> I suggest you review the data you are recieving prior to using it.  
> Use print\_r($\_POST) to review all the data you are receiving and then decide how you are going to process the input.
> 
> [color=black]If I recall correctly you will be getting multiple $\_POST[“entry\_type”] data pairs and you need to process all of them. You may want to concantenate them with $entry\_type .= $\_POST[“entry\_type”]; in a for loop or you may want to store each individually. Depends upon desire to do a look on the data and ease of displaying it.[/color]

Good suggestion. You won’t get multiple values if you don’t use entry\_type as an array in the form though.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 12:23am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/7 "2004-07-08T00:23:02Z")

</div>

Thanks all for your help.

I used print\_r($\_POST) to see what I was sending, and it got me a little further. The data with multiple values prints like so:

…[entry\_type] =\> Array ( [0] =\> homepage [1] =\> investor …

where I’ve checked the “homepage” checkbox and “investor” checkbox. Then the data that appears in the mysql table is “Array”, not “homepage” or “investor”.

So using my actionscript knowledge, I included a for loop in the sql query on the php page, so the page goes:

```php

<?
require 'include.php';

$entry_date = $_POST["entry_date"]; 
$entry_title = $_POST["entry_title"]; 
$entry_summary = $_POST["entry_summary"]; 
$entry_body = $_POST["entry_body"];
$entry_type = $_POST["entry_type"]; 

//print_r($_POST);

$cid = mysql_connect($DBhost,$DBuser,$DBpass);
mysql_select_db($DBName); 
if (!$cid) { print "Error: " . mysql_error() . "
"; }

if ($_SERVER['REQUEST_METHOD'] == "POST") {

$sql = " INSERT INTO itl_news (";
	$sql .= "entry_date, "; 
	$sql .=	"entry_title, "; 
	$sql .=	"entry_summary, ";
	$sql .=	"entry_body, "; 
	$sql .=	"entry_type)";
	$sql .=	"VALUES ";
	$sql .= "('$entry_date', ";
	$sql .=	"'$entry_title', ";
	$sql .=	"'$entry_summary', ";
	$sql .=	"'$entry_body' ";
for($i = 0; $i <= 1; $i++)
	{	
	$sql .= " '$entry_type[$i]')";
	}
$result = mysql_query($sql, $cid);

if (mysql_error()) { print "Database Error: $sql " . mysql_error(); } 

print "New Entry Added for <b>$entry_date</b> ($entry_type announcement).";

mysql_close();

}
?>

```

By the look of it, my application of the for loop is incorrect (as my printed data tells me) and it creates bad syntax in the sql query.

I just want to add a string of values to the entry\_type column in the mysql table. Eg. “homepage investor” or “investor” or “homepage investor customer”.

Thanks so far for all your help.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 2:07am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/8 "2004-07-08T02:07:40Z")

</div>

if this is all going to the same table field you could easely use serialize()

```php

#example
$entry_type = serialize($_POST["entry_type"]); 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:22am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/9 "2004-07-08T03:22:13Z")

</div>

Hey thanks, that works. I had a quick look on [php.net](http://php.net) for a string slice function but only found array\_slice. So what’s the best way to get rid of the “s:8” (it’s the number of characters in the posted value)? It’s easiest in the end if my value just equals the value I gave the checkbox.

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 3:26am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/10 "2004-07-08T03:26:33Z")

</div>

dont get rid of it… when you retrieve it just unserialize() it 🙂 and off you go…

```php

 $entry_type =unserialize($row['fieldname']);
 echo $entry_type;
 

```

[color=#000000][color=#007700]

[/color][/color]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 8, 2004, 7:50am UTC](https://forum.kirupa.com/t/mysql-php-sending-multiple-checkbox-values/56609/11 "2004-07-08T07:50:33Z")

</div>

Thanks heaps, got it sorted with that last one!
