Click to See Complete Forum and Search --> : What is the correct datatype for checkbox?


rreze
12-13-2010, 12:11 PM
Hi there,

I am using checkbox option in my form. The user has to select 2 to 3 options. I don't know which data type to use in mysql database and is there any way I can make the user to choose min 2, max 3 options?

Thanks.

thraddash
12-13-2010, 12:37 PM
For mySQL, you could either use the TINYINT(1) data type for each checkbox in the form, or you could use a single INTEGER/TINYINT column (depending on how many you have) to store all the checkbox values using bitwise operators, I normally call that column "flags".

eg
<input type="checkbox" name="myValue[]" value="1" />
<input type="checkbox" name="myValue[]" value="2" />
<input type="checkbox" name="myValue[]" value="4" />

As for the min and max checks, I am going to suggest using JavaScript to validate how many checkboxes are chosen.