Jump to content

can i put multiple checkboxes in to 1 db column?


ricky spires

Recommended Posts

hello.

can (or rather how can) i put multiple checkboxes in to 1 db column?

 

so, can i do this in 1 table:

 

id - tick box id

1  - 1,2,3

2  - 4,5,6

3  - 7,8,9

 

and then relate that table to another table

 

tick box id  -  name

1                -  name1

2                -  name2

3                -  name3

etc..

 

 

if so,

what format would i use in the db?  varchar, enum or set  (1,2,3 or '1','2','3')

and

how would i echo them out with the names from the second table?

 

thanks

 

Link to comment
Share on other sites

Don't, use one row for each tick box.

 

ID | Tickbox ID |
----+------------+
  1 |     1      |
  1 |     2      |
  1 |     3      |
  2 |     4      |
  2 |     5      |
  2 |     6      |
....

 

Trying to store them in one field separated by commas is only going to cause you major pains in the future if you need to do anything with those values, such as search on them or join them to another table.

 

Link to comment
Share on other sites

thankyou for your reply :)

 

i did get it working by putting them in to seprate rows but i could see a problem.

 

what would would happen if i had 20, 30 or even 50 tick boxes ?

then it would start to get messy.

 

for example:

 

a table for tick boxes

 

so, can i do this in 1 table:

id

tick box name

1

name 1

2

name 2

3

name 3

etc up to 50

[/td]

 

 

 

and then another table of users (for example)

 

and user 1 ticked 20 of the 50 boxes. i would need to create seprate 20 rows for each tick box.

 

like:

 

[td]user_id

tick boxes selected

1

tick box1

1

tick box2

1

tick box3

1

tick box4

1

tick box5

2

tick box1

2

tick box2

3

tick box3

3

tick box4

3

tick box5

etc etc etc etc etc etc

 

so the problem is:

when they press sumit how would it create all those separate tick box rows

 

i guess when viewing the user you would you use something like

$sql = "SELECT * FROM "tick box table" WHERE id=".$tick_box_id."";

Link to comment
Share on other sites

when they press sumit how would it create all those separate tick box rows

 

You have your form submit an array of all the checkboxes that they have checked.  Then on the PHP side you do two things

1) Delete all the current entries for that user, eg DELETE FROM checkboxTable where ID=$userId

2) Loop over the array of checkboxes submitted and then generate and execute an INSERT statement for each one.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.