Hello everyone,

I'm sure this has a quick answer/fix but I can't seem to figure out how to store a checkbox value to mysql. And I'm not sure what type I should be saving it as in the field on mysql(like bool, int, varchar).

I have 14 check boxes and I need to see if they're checked and store that info into my data base using mysql. If possible too I'd like it instead of saying "checked/unchecked or yes/no" if it could say what the name of the checkbox is. Hope this made sense. Thanks in advance.

I usually group them into arrays and enter them into the database as a comma separated list which would be a varchar.

<form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
<p><input type="checkbox" name="sports[]" value="football" />&nbsp;football<br />
<input type="checkbox" name="sports[]" value="hockey" />&nbsp;hockey<br />
<input type="checkbox" name="sports[]" value="baseball" />&nbsp;baseball</p>

<p><input type="checkbox" name="soda[]" value="pepsi" />&nbsp;pepsi<br />
<input type="checkbox" name="soda[]" value="coke" />&nbsp;coke<br />
<input type="checkbox" name="soda[]" value="rootbeer" />&nbsp;rootbeer</p>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<?
if(count($_POST) > 0)
{
	print_r($_POST['sports']);
	echo "<br />";
	print_r($_POST['soda']);
	echo "<br />";
}
?>

i think using implod() function is a better idea:idea:

I usually group them into arrays and enter them into the database as a comma separated list which would be a varchar.

<form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
<p><input type="checkbox" name="sports[]" value="football" />&nbsp;football<br />
<input type="checkbox" name="sports[]" value="hockey" />&nbsp;hockey<br />
<input type="checkbox" name="sports[]" value="baseball" />&nbsp;baseball</p>

<p><input type="checkbox" name="soda[]" value="pepsi" />&nbsp;pepsi<br />
<input type="checkbox" name="soda[]" value="coke" />&nbsp;coke<br />
<input type="checkbox" name="soda[]" value="rootbeer" />&nbsp;rootbeer</p>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<?
if(count($_POST) > 0)
{
	print_r($_POST['sports']);
	echo "<br />";
	print_r($_POST['soda']);
	echo "<br />";
}
?>

Ok I've made them into an array but how would I sort each one being comma separated and store that to the DB?

How do you want to save it in the table ? comma separated selected values in one row ?

How do you want to save it in the table ? comma separated selected values in one row ?

Yeah comma separated for the ones that are checked only ;)

$string = $string = implode(",",$_POST['checkboxarray']);
$query = "insert into table (columnname) values ('$string')";
mysql_query($query);

But wouldn't it cause a problem ? If you want to retrieve the values and display again the selected values, you have to fetch the row from the table, explode it to an array and then see if a particular sport is in this array. If yes, then "check" it, else, do nothing!

$string = $string = implode(",",$_POST['checkboxarray']);
$query = "insert into table (columnname) values ('$string')";
mysql_query($query);

But wouldn't it cause a problem ? If you want to retrieve the values and display again the selected values, you have to fetch the row from the table, explode it to an array and then see if a particular sport is in this array. If yes, then "check" it, else, do nothing!

I got it to work! Thanks =D My method was a little different but still works the same. As for taking it out of the table I was using the myphpadmin to export it to a file. Thanks again =)

function check1($var){if(!preg_match("/[^A-Za-z]/",$var)) {return TRUE;} else   {return FALSE;}}
// CHECK POST VALUE EXIST
if(isset($_POST["mods"])) {$mods=$_POST["mods"];
 // IF YES, COUNT ARRAY
$nb_modss=count($mods);
// MAKE SURE THAT EVERY VALUE OF THE ARRAY IS [a-z]
for ($i="0"; $i<$nb_mods; $i++) {
if(!check1($mods[$i])) {$mods[$i]="";}} unset($nb_mods);}
// IF NOT, ARRAY NULL
else {$mods=array();}

$mods = implode(", ", $mods);

:) Cool !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.