我正在尝试将动态生成的输入字段数组插入到我的数据库中。 问题是在插入过程中插入变得混乱。当表单中的某些字段为空时,某些记录会插入到错误的行中。 我在某处必须正确索引我的数组。我不知道该怎么做。有没有更好的方法呢?或者有办法修复我的代码。
我到处寻求帮助,但我找不到任何帮助。如果我能从这里的专家那里得到一些帮助,我将非常感激。
这是表格
<?php
while($row2=$coursefetch1->fetch()){
$iid = $row2['id'];
if($row2['type'] == 0){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="checkbox" name="yanswer1[]" value="a"><span> '.$row2['a'].' </span><br>
<input type="checkbox" name="yanswer2[]" value="b"><span> '.$row2['b'].' </span><br>
<input type="checkbox" name="yanswer3[]" value="c"><span> '.$row2['c'].' </span><br>
<input type="checkbox" name="yanswer4[]" value="d"><span> '.$row2['d'].' </span><br>
</div>
</div>
';
}
elseif($row2['type'] == 2){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="radio" name="yanswer1[]" value="a"><span> '.$row2['a'].' </span><br>
<input type="radio" name="yanswer2[]" value="b"><span> '.$row2['b'].' </span><br>
<input type="radio" name="yanswer3[]" value="c"><span> '.$row2['c'].' </span><br>
<input type="radio" name="yanswer4[]" value="d"><span> '.$row2['d'].' </span><br>
</div>
</div>
';
}
elseif($row2['type'] == 1){
echo '
<div class="tab-pane" id="'.$row2['id'].'">
<div class="row" align="center">
<b> '.$row2['question'].' </b><br>
<input type="hidden" name="question[]" value="'.$row2['question'].'">
<input type="hidden" name="test_id[]" value="'.$row2['test_id'].'">
<input type="text" name="yanswer1[]" value="">
</div>
</div>
';
}
}
?>
这是处理表单
的文件 <?php
require "head.php";
if (isset($_POST['submit'])) {
$question_array = $_POST['question'];
$testid_array = $_POST['test_id'];
$answer_array1 = $_POST['yanswer1'];
$answer_array2 = $_POST['yanswer2'];
$answer_array3 = $_POST['yanswer3'];
$answer_array4 = $_POST['yanswer4'];
$i = 0;
for($i = 0; $i < count($question_array); $i++) {
$question = $question_array[$i];
$testid = $testid_array[$i];
$answer = $answer_array1[$i]."".$answer_array2[$i]."".$answer_array3[$i]."".$answer_array4[$i];
$enterscore = $achilles->prepare("INSERT INTO `user_answer` (test_id,question,your_answer)
VALUES (:t,:q,:a)");
$enterscore->bindparam(':t',$testid);
$enterscore->bindparam(':q',$question);
$enterscore->bindparam(':a',$answer);
$enterscore->execute();
}
}
?>
答案 0 :(得分:0)
...
<input type="hidden" name="test_id" value="'.$row2['test_id'].'">
<input type="radio" name="yanswer1" value="a"><span> '.$row2['a'].' </span><br>
<input type="radio" name="yanswer2" value="b"><span> '.$row2['b'].' </span><br>
<input type="radio" name="yanswer3" value="c"><span> '.$row2['c'].' </span><br>
... 在其他地方比喻
编辑: ...
if (!empty($_POST['submit'])) {
...