我有三个字段
<textarea rows="2" name="answer[]" ></textarea>
<select name="fraction[]">...
<textarea rows="2" name="feedback[]"></textarea>
用户应至少四次填写此字段一次以上 然后我使用php循环遍历此字段以将其插入数据库
$answer = isset($_POST['answer']) ? $_POST['answer'] : "" ;
$fraction = isset($_POST['fraction']) ? $_POST['fraction'] : "" ;
$feedback = isset($_POST['feedback']) ? $_POST['feedback'] : "" ;
foreach($answer as $key=>$value){
$answer = $value;
$fraction = $fraction[$key];
$feedback = $feedback[$key];
$query = "insert into `question_answer` ( answer, fraction, feedback) values ('$answer', '$fraction','$feedback')";
$questions->insertData($query,$con);
}
这个插入数量的记录,第一个记录包含我想要的所有值,但其他记录只包含与数组i循环相关的字段的值,其他字段为空。任何帮助??
答案 0 :(得分:2)
你在第一个循环中覆盖你的变量......拿这个:
$answer = isset($_POST['answer']) ? $_POST['answer'] : "" ;
$fraction = isset($_POST['fraction']) ? $_POST['fraction'] : "" ;
$feedback = isset($_POST['feedback']) ? $_POST['feedback'] : "" ;
foreach($answer as $key=>$value){
$query = "insert into `question_answer` ( answer, fraction, feedback) values ('$value', '$fraction[$key]','$feedback[$key]')";
$questions->insertData($query,$con);
}