简体   繁体   中英

update comma values in mysql

i have a product table in mysql, one of the field (attributes field ) contain comma values like attibutes_1,attibutes_2 etc. in my form i have a checkboxes foreach attributes to my product, the html like this

<input type="checkbox" value="attributes_1" name="attributes[$id][]">
<input type="checkbox" value="attributes_2" name="attributes[$id][]">

and my question are,...how can i update those attributes values ? if try this with implode

  $coma_values = implode(',',$_POST['attributes'][$id]);

and update it to the table , but sometimes doesn't works perfect. Please help me for better solution

you need a separate table for attributes, then link the two tables with an id field.

table 1:
id, somedata1, somedata2

table 2:
id, table1_id, attribute_name, attribute_value

or something like that. table1_id will have values that correlate with a matching id in the first table. this way you can have a one to many relationship between records and attributes. this will allow you to use a query with JOINs to pull up data from both tables in one single result set. you should never store comma separated values in one field.

You will never have a solution that works perfectly, or even very well, if you violate basic relational database theory by combining multiple data into a single column. Since you don't state what your actual problem is no one will be able to help you solve it, but I'm guessing that it is either commas contained within things that should be single expressions or else overrunning the field length.

The solution is to store those attributes in one or more subsidiary tables related back to the main data table using the primary key of the data table. For a better idea how to do that, you'll have to specify what exactly is stored in the attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM