Okay, what I'm doing should be fairly simple, but I've been working at it for a couple of hours, I've read books, I've been on php.net, and I'm not getting it right.
Essentially, I am allowing someone to create a survey. They can add a question, and then they can add n answers. The answers have a response and an order. So, the form looks like this:
<tr>
<td>Answer</td>
<td><input type="text" name="answer[response]" size="50"></td>
</tr>
<tr>
<td>Order</td>
<td><input type="text" name="answer[order]" size="5"></td>
</tr>
The form can have as many answers as they want. Each time they add an answer, another blank space appears for a new answer. So, once submitted, the page first erases any existing answers and then tries to do this:
foreach ($_POST['answer'] as $a => $v1) {
if ($answer['response'] != "") {
$sql_addAnswer = "
INSERT INTO survey_answer (
survey_section_id,
survey_question_id,
survey_answer,
survey_answer_order
) VALUES (
'$_POST[survey_section_id]',
'$sid',
'$answer[response]',
'$newAnswer[order]'
)";
if ($res_addAnswer = mysql_query($sql_addAnswer))
$addAnswer = true;
}
}
But it's not working. If my first answer is male / 1 and my second answer is female / 2, they both show up as male / 1. Or when I have only one answer, I get it more than once.
What am I doing wrong? Can someone help?
Thanks.