MySQL, PHP: sending multiple checkbox values

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

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:


<?
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.

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


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