Storing a PHP Checkbox in a Database

meerecat1981 used Ask the Experts™

I have a PHP page with text and Database fields.

This is the code that draws the checkbox:

    <td width="43"><label for="business_type">Salon</label></td>
    <td width="30"><input type="checkbox" name="business_type[]" id="business_type" value="salon" /></td>
    <td width="38"><label for="business_type">Mobile</label></td>
    <td width="30"><input type="checkbox" name="business_type[]" id="business_type" value="mobile" /></td>
    <td width="53"><label for="business_type">Student</label></td>
    <td width="25"><input type="checkbox" name="business_type[]" id="business_type" value="student" /></td>
    <td width="37"><label for="business_type">Other</label></td>
    <td width="434"><input type="checkbox" name="business_type[]" id="business_type" value="other" /></td>

Open in new window

This is the code that stores the texts fields in the database:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

 mysql_connect("localhost", "web46-training", "training") or die(mysql_error()); 
 mysql_select_db("web46-training") or die(mysql_error()); 
 mysql_query("INSERT INTO `data` VALUES ('$trainee_name', '$company_name', '$address', '$telephone', '$email_address', '$website', '$trainer_name', '$training_date', '$trainer_notes', '$area_consultant')"); 
 Print "Your information has been successfully added to the database."; 

Open in new window

How do I store which of the php checkboxes are checked into the database.  What kind of field do I need to create in the database table.  All my text fields are Varchar.  I was thinking maybe bit?

Many Thanks
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
In your specific case an SET with values 'salon','mobile','student','other' sounds appropriate to store all four checkbox values in a single database field.
Then again, I suspect that you really want to use radiobuttons rather than checkboxes (in that case use ENUM instead of SET).
In both cases you have to use the correct insert syntax for the field type of course, but the advantage over a bit is that you can use meaningful content directly generated from the business_type[] array
You can concatenate with | or , the selected values and store them in text filed. So that you can split the entries back.
BTW, you do know that you should be more careful with handing input data to mySQL?
What if someone maliciously entered
foo'); truncate table data;

Open in new window

in the consultant field?
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.


Are you able to show me an example? I'm new to this.

Enterint the above code did nothing to the database.  Isn't that because a VARCHAR field is purely for storing text? Please feel free to tell me i'm talking rubbish lol.
Most Valuable Expert 2011
Top Expert 2016
I would create four columns of type VARCHAR.  I would put the values from the checkboxes into these columns.  That way your data base table will be easier to read.  If you use separate columns, it will be easy to add more definitions (with ALTER TABLE) and all your existing queries will still work after you do that.

If you do not already know about it, here is a helpful tool:

A good book to help you get started learning PHP and MySQL is available here:

You definitely need to learn about this PHP function.  While the comment from thehagman at ID: 38001662 is not really applicable (MySQL_Query() will not run more than one query) it is still a good practice to filter and escape your external data.

You also need to learn about this function.  MySQL is not a black box; it can and will fail, sometimes for reasons outside of your control.  So you must test the return values from its functions and (immediately) visualize the error information.

Best of luck with your project, ~Ray
I would prefer the data type as 'text' .

$chkbox = $_REQUEST['business_type'];

$chboxval = implode(",",$chkbox);

insert query.....
Most Valuable Expert 2011
Top Expert 2016

I tried to post a link to this article earlier, but apparently it did not "take," so here it is...

HTH, ~Ray
Most Valuable Expert 2011
Top Expert 2016

Wow, what was wrong with the answers?  You did not even ask for clarifying information - you just gave a marked-down grade.  Please see the grading guidelines here:

Please tell us what you expected and why you didn't respond if you thought our posts did not have enough information for your needs.  thanks, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial