0

I am new to PHP and MySQL, please I would appreciate your help. I have a table that generates several rows depending on the number of options selected by a user. I want to store the content in a database table after the user presses the submit button. Since i cannot tell how may options the user would select and how many rows will be generated, i don't know how to write the SQL code that will store it in my database table.

here is the code to generate the table

for($x=0;$x<$N; $x++) {

        echo nl2br("<td><textarea name=art[] rows=10 cols=30></textarea></td><td><textarea name=science[] rows=10 cols=30></textarea></td></textarea></td><td><textarea name=method[] rows=10 cols=30></textarea></td><td><textarea name=criteria[] rows=10 cols=30></textarea></td></tr>"); 
}

To get the data from the table

for($i = 0; $i <$N; $i++){
    $data[] = array(($art[$i]), ($science[$i]), ($method[$i]), ($criteria[$i]));
}
MinJ
  • 11
  • 2
  • Can you please show me the html and also mention the conditions or any live url to check – Roger Jan 30 '13 at 10:52
  • mysql can store objects. I don't know about php. look at this http://dev.mysql.com/doc/refman/5.0/en/blob.html and also http://stackoverflow.com/questions/5285857/when-is-using-mysql-blob-recommended – Bhavik Shah Jan 30 '13 at 11:07
  • @BhavikShah I know mysql can store objects. my question is how to write the code that will store the content from a generated table array – MinJ Jan 30 '13 at 12:56

2 Answers2

0

I would normalize this design and have a USER and OPTION table with a one-to-many relationship between them. You have as many rows as you do selections that way. You can query later to see how many USER rows chose OPTION X.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • please can you elaborate on the one-to-many relationship? give example if possible. thanks – MinJ Jan 30 '13 at 12:51
  • You should understand it better than me: a USER can select zero or more OPTIONs. You'd have a row in the USER table and one for each selected OPTION, with a foreign key in the OPTION table pointing back to the USER primary key. – duffymo Jan 30 '13 at 13:15
0

You can merge (implode) all values and store into table. split ( explode ) the values while retrive.

Store : 
$optionsVal = implode(",", $data);

Retrieve :
$data = explode(",", $optionsVal);
Siva Karuppiah
  • 995
  • 1
  • 8
  • 17