I have a dynamic form generated by a while loop that looks like this:
<form>
<?php while ($questions = $query->fetch(PDO::FETCH_ASSOC)) { ?>
<textarea name="reponse_text[]"></textarea>
<input type="hidden" name="question_id[]" value="<?php echo $questions['question_id']; ?>">
<?php } ?>
</form>
I am trying to insert each 'response' and 'question_id' into its own row. I believe using a forloop to do this is necessary but can't figure out how to access each POST array value:
$user_checkup_id = $db->lastInsertId();
$query = $db->prepare("INSERT INTO user_responses (user_response_text, question_id, user_checkup_id)
VALUES (:user_response_text, :question_id, :user_checkup_id)");
foreach ($_POST as $key => $value) {
$query->bindValue(':user_checkup_id', $user_checkup_id, PDO::PARAM_INT);
if($key == "response_text") {
$query->bindValue(':response_text', $value, PDO::PARAM_STR);
} else if($key == "question_id") {
$query->bindValue(':question_id', $value, PDO::PARAM_INT);
}
}
$query->execute();
The value submitting to the database is "Array" for response_text. How do I access the actually text area value for each row?