这与测验应用程序有关。问题从数据库中随机生成。用户通过单选按钮选择一个答案。答案与问题ID和用户ID一起存储在数据库中。 一切工作正常。在Codeigniter中的数据库中的数组中插入数组
问题是我有一个数组,我想存储在我的数据库中。以下是代码。
的观点:
<div class="col-lg-8">
<table class="table" style="width: 100%;">
<?php $i = 0;
$ques = 1;
echo form_open ('Menu/submit_ans', array ('name' => 'quiz'));
foreach ($quiz_array as $q) { ?>
<td colspan="2" style="background-color: #337ab7; color: white;">
<h4>Question No. <?php echo $ques?> </h4>
</td>
<tr>
<td colspan="2"><?php echo $q->ques;?></td>
<input hidden name="qid[]" type="text" value="<?php echo $q->qid;?>">
<input hidden name="uid[]" type="text" value="<?php echo $user['id'];?>">
</tr>
<tr>
<td><?php echo $q->ans_1;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="1"></td>
</tr>
<tr>
<td><?php echo $q->ans_2;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="2"></td>
</tr>
<tr>
<td><?php echo $q->ans_3;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="3"></td>
</tr>
<tr>
<td><?php echo $q->ans_4;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="4"></td>
</tr>
<?php $i ++;
} ?>
</table>
<center>
<button class="btn btn-success" type="submit">Submit!</button>
<a></a>
</center>
</div>
<?php echo form_close();?>
控制器:
function submit_ans() {
$data = array(
'qid' => $_POST['qid'],
'user_id' => $_POST['uid'],
'ans_att' => $_POST['ans']
);
$this->load->model('MyModel');
$this->MyModel->insert_ans($data);
}
型号:
function insert_ans($data) {
foreach($data as $answer) {
//var_dump($answer);
}
$this->db->insert_batch('tbl_answers', $data);
}
以下是错误我得到:
INSERT INTO `tbl_answers`() VALUES ('6','9','1','5','7','10','4','12','8','3','11','2'), ('1','1','1','1','1','1','1','1','1','1','1','1'), Array
如何将数组存储到我的数据库中?
use like that'ans ['ans_att'] [<?php print $ i; ?>]'全部相同 – devpro