Jump to content

Array with Textfield


verdrm

Recommended Posts

I have a bunch of text fields with the same purpose (ex: responses to a ballot question), and I would like the insert them into MySQL. The number of text fields, however, can fluctuate because I have a JavaScript function to add more text fields. I assume I would use an array to get the # of text fields and then insert them?

Link to comment
Share on other sites

Use the same form field name with empty square brackets to get an array of values.

 

<input type="text" name="ballot_answer[]" />
<input type="text" name="ballot_answer[]" />
<input type="text" name="ballot_answer[]" />
<input type="text" name="ballot_answer[]" />

 

Then in PHP, $_POST['ballot_answer'] will php a numeric array you can work with.

 

Does that help?

Link to comment
Share on other sites

How would I take all the values in the array and insert them once in the same MySQL record with commas?

 

For example, if I have response a,b,c,d....instead of inserting each in a new record how would I just do: a,b,c,d in one record?

Link to comment
Share on other sites

mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')");

As far as I am aware that will not work.

 

Why not? Implode just makes a string, that is then stored into the 'answers' column, which will need to be of datatype varchar or text

Link to comment
Share on other sites

Both should work. However I'd go with storing answers in separate rows instead of all in a single comma-separated field. It's a much better db design.

 

[pre]

Answers  Method A

-----+----------+--------+

user | question | answer |

-----+----------+--------+

  1  |    1    |  a    |      Q  Which users answered "c" to Q2 ?

  1  |    2    |  c    |

  1  |    3    |  b    |      A      SELECT user FROM answers 

  1  |    4    |  a    |              WHERE question=2 AND answer='c'

  1  |    5    |  c    |

  2  |    1    |  c    |

  2  |    2    |  a    |

  2  |    3    |  b    |

  2  |    4    |  c    |

  2  |    5    |  b    |

  3  |    1    |  b    |

  3  |    2    |  c    |

  3  |    3    |  b    |

  3  |    4    |  a    |

  3  |    5    |  b    |

 

Answers    Method B

-----+-------------------+

user | answers          |

-----+-------------------+

  1  | a,c,b,a,c        |      Q  Which users answered "c" to Q2 ?

  2  | c,a,b,c,b        |

  3  | b,c,b,a,b        |      A  Ermmmm!

Link to comment
Share on other sites

mysql_query("INSERT INTO `tblname` (answers) VALUES ('".implode(',',$_POST['ballot_answer'])."')");

As far as I am aware that will not work.

 

Why not? Implode just makes a string, that is then stored into the 'answers' column, which will need to be of datatype varchar or text

As Barand said, both will work. But this one is better than the other:

foreach ($_POST['ballot_answer'] as $value)
{
mysql_query("INSERT INTO `tblname` (answers) VALUES ('".$value."')");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.