jeff5656 Posted January 14, 2009 Share Posted January 14, 2009 If I have a drop down where a user can select multiple items (or a check box to check more than one) how do I store that in the database? Do I *have* to create a second table and then JOIN the two tegether or is there a way of doing it with only one table? Quote Link to comment https://forums.phpfreaks.com/topic/140847-does-mutliple-selects-need-a-second-table/ Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 depending on the situation, it can sometimes be acceptable to just join the data with a character (like a comma) then split on the data to read it back. instead of join, you can also use serialize()/unserialize() to turn an array into a string and back. this way uses more characters, but is safer Quote Link to comment https://forums.phpfreaks.com/topic/140847-does-mutliple-selects-need-a-second-table/#findComment-737207 Share on other sites More sharing options...
flyhoney Posted January 14, 2009 Share Posted January 14, 2009 I agree with rhodesa. However, the most extensible solution uses three tables. table items id int(11) primary key auto_increment name varchar(32) table users id int(11) primary key auto_increment table users_items id int(11) primary key auto_increment item_id int(11) user_id int(11) And then to get a user's choices: SELECT items.* FROM items LEFT JOIN users_items ON users_items.item_id = items.id WHERE users_items.user_id = 828 This would get the items associated for user id = 828 Quote Link to comment https://forums.phpfreaks.com/topic/140847-does-mutliple-selects-need-a-second-table/#findComment-737212 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 I agree with rhodesa. However, the most extensible solution uses three tables. table items id int(11) primary key auto_increment name varchar(32) table users id int(11) primary key auto_increment table users_items id int(11) primary key auto_increment item_id int(11) user_id int(11) And then to get a user's choices: SELECT items.* FROM items LEFT JOIN users_items ON users_items.item_id = items.id WHERE users_items.user_id = 828 This would get the items associated for user id = 828 I love 3NF databases! Quote Link to comment https://forums.phpfreaks.com/topic/140847-does-mutliple-selects-need-a-second-table/#findComment-737213 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.