This is driving me nuts!!! Is there anyway to have one checkbox with one value in a form, that simply stores that single value into a single field in mysql... without using any arrays or imploding / exploding things and all that nasty stuff. I just want to seperate the values into their own fields!

Thanks in advance.

Member Avatar for diafol

The checkbox 'id/name' attribute, eg 'mybox' will be $_POST['mybox'] . If the checkbox is checked, it $_POST['mybox'] will be set (true), if not, not. The only way your checkbox will be part of an array is when you have more than one checkbox with the same 'name' attribute. If you ensure that each checkbox has an unique 'name', should be easy.

The checkbox widget can only have one HTML 'checked' attribute value: checked = "checked" . In other words it can only be on or off (i.e. true or false).

Can't see why you'd need to use any array functions.

The checkbox widget can only have one HTML 'checked' attribute value: checked = "checked" . In other words it can only be on or off (i.e. true or false).

Can't see why you'd need to use any array functions.

Thanks for the help, how do i store that true/false in a mysql field?
I can't seem to get my checkboxes to store anything in my database, do i need to set the field to BOOL or something?

Member Avatar for diafol

To create a bool field, don't use BOOL use tinyint and set true values to 1 (default = 0). With your $_POST['mybox'] :

If(isset($_POST['mybox'])){
  $check = ", myboolfield = '1'";
}else{
  $check = "";
}
//Your query could then be something like:
$sql = "INSERT INTO mytable SET field1 = '{$f1}', field2 = '{$f2}'{$check}";

Where myboolfield is the name of your database field for your checkbox. The other form variables should be cleaned:

$f1 = addslashes(htmlentities($_POST['f1']));

//etc.

In addition, you should validate the data (check to see if date values are date values, integeres are integers etc.) before running the query.

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.