我有一个表格,其中最多包含10组单选按钮。单选按钮提供问题的答案。从数据库中填充所有问题,完成后,将使用字段user_id,question_number和answer_value更新数据库。我正在使用一个数组,但是似乎只有一个answer_value传递给表单处理器。
这是我的代码
<form action="new-answers.php" method="post" id="my_answer">
<div class="form-group">
<label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="1"> Totally false for me </label>
<label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="2"> Partially false for me </label>
<label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="3"> Indifferent/neutral </label>
<label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="4"> Partially true for me </label>
<label class="radio-inline"><input name="answer_value[]" type="radio" required="required" value="5"> Totally true for me </label>
这是我在表单处理器页面中使用的
if (isset($_POST['submit'])) {
$i=0;
$user=$_POST['user_id'];
$question=$_POST['question_number'];
$answer = $_POST['answer_value'];
// open the mysqli prepared statement globally
$stmt = $mysqli -> prepare('INSERT INTO answers (user_id,question_number,answer_value) VALUES (?, ?,?)');
function insertAnswer($user, $question, $answer) {
global $stmt;
// using prepared statement several times with different variables
if (
$stmt &&
$stmt -> bind_param('iii', $user, $question,$answer) &&
$stmt -> execute()
) {
// new answers added
}
}
}
即使我尝试使用
进行回显$answer = $_POST['answer_value'];
foreach($answer as $value) {
echo $value.'<br />';
}
我只能得到答案。