ricky spires Posted December 5, 2011 Share Posted December 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/ Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/#findComment-1294699 Share on other sites More sharing options...
ricky spires Posted December 5, 2011 Author Share Posted December 5, 2011 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.""; Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/#findComment-1294719 Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/#findComment-1294737 Share on other sites More sharing options...
xyph Posted December 5, 2011 Share Posted December 5, 2011 Single query to insert multiple rows INSERT INTO `table` ( col1, col2 ) VALUES ( 'data1a', 'data1b' ), ( 'data2a', 'data2b' ) etc. Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/#findComment-1294745 Share on other sites More sharing options...
ricky spires Posted December 6, 2011 Author Share Posted December 6, 2011 Great. Thanks for all your help. I'll give it a go r Quote Link to comment https://forums.phpfreaks.com/topic/252521-can-i-put-multiple-checkboxes-in-to-1-db-column/#findComment-1294832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.