checkbox values inserting to mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Locked
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

checkbox values inserting to mysql

Post by zyklone »

I have a form with checkboxes on it. if the user checks two or more checkboxes i use to compress all values in string format (example: dog|cat|chicken) then thats the time i will store it to my mysql database. it saves my database size coz instead of putting doc, cat, chicken in each row of my table i use to compress it.

is it a good practice? or what other technique to you use? thanks!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

make your checkboxes an array then serialize() the array for insertion to the db.
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

Post by zyklone »

if i use serialize() how can i query it? does the mysql can still find it?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

theoretically you 'could' using the like operator, but that's not ideal.

I don't think you're any better off if you want to save them in the same column using a different method, but a cleaner solution would be to use implode() if you're planning on searching on those values...it's cleaner, but still not your 'best' solution.

you have two threads going that are basically asking the same questions.... I'm locking this one and you can continue any further discussions in the other.
Locked