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
https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497601
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
https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497609
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
https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497713
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
https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497775
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
https://forums.phpfreaks.com/topic/96821-array-with-textfield/#findComment-497780
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.