#native_company# #native_desc#

Storing Checkbox Data in a Database

By Dan LaFlamme
on October 15, 2000


Checkboxes in web forms are useful because they allow the person filling out the form
to choose from a wide range of possible choices.
Specifically, it allows the user to choose some, all or none of the elements. Although
checkboxes are excellent form elements for certain tasks,
there is often confusion about how to store checkbox data correctly. This article will
demonstrate how to store checkbox data in a database, in a manner that follows good
database design principles.


In this article, I will demonstrate the method of storing checkbox data on a per user
basis. Although there is useable PHP source code included,
I’m presenting this in a database design point of view. Therefore, the information
here can be used with any database and server side
scripting language of choice. The main idea here is to provide you with an explanation
of how to do it so you can apply it to the sites you build. If you do want to play with the
source code, you’ll need php, mysql, and a webserver.

Example: A Job Posting Site

Consider the following situation. You are asked to create a web site that will allow
unemployed web developers to post their skills there. Potential employers can then visit
this website and search for prospective employees, based on the skills they are looking
for. As an aspiring web developer yourself, you know that a web programmer
with work is more desirable than one without. Therefore, you decide to write the code for his
Each unemployed web developer will presumably visit the site, create an account, and enter his
skills in some way or another. Checkboxes immediately come to mind.
You envision a page that looks something like this:
  __ PHP     __ MySQL        __ Zope
  __ Perl    __ Javascript   __ JSP

Each developer goes through and checks the boxes that match his skills. Obviously, different developers will check different boxes. One may check PHP and MySQL,
while another checks only JSP. How will you store this data? It is natural to want to store each developer’s list of skills in the user table shown above. After all, each user
will have a row in this table and you can just make some fields in each row to hold their skills. This may work at first, but you will almost surely run into trouble at
some point when you wish to expand or modify the database. Also, your user table would be unnecessarily wide. There is a better way to do this. Here is how.