我正在制作一个测验应用程序,其中一个问题必须是正确的。我已经可以在我的表中插入多个数据,我遇到的问题是,我有一个特定的选择或答案,必须与其他选择或答案不同。
该应用程序的屏幕截图以及我想要做什么
这是我的桌子的样子
table answers
id | answer | correct
1 | Apple | 1
2 | Mango | 0
3 | Melon | 0
1 表示正确答案,0 表示不正确。
所以这是我的模型。
我试图尝试获取作为值的单选按钮值并将1其插入数据库,但结果是,如果我添加另一个数据或多个数据。索引 0 或第一个数据是唯一可以插入的数据。我不能选择我可以检查的单选按钮,只能检查我可以检查的第一个按钮。
// Insert questions
$field_question = array(
'question'=>$this->input->post('question'),
);
$this->db->insert('questions', $field_question);
// Insert Answers
$data_answer = $this->input->post('choice[]');
$data_is_correct = $this->input->post('checkChoice[]');
$value = array();
for($i = 0; $i < count($data_answer); $i++) {
$value[$i] = array(
'answer' => $data_answer[$i],
'correct' => $data_is_correct[$i],
'question_id' => $this->db->insert_id(),
);
}
$this->db->insert_batch('answers', $value);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
如果我添加 3 个新数据,我的表会出现问题
id | answer | correct
1 | Apple | 1
2 | Mango | 0
3 | Melon | 0
* New 3 Data Inserted
4 | Orange | 1
5 | Tomato | 0
6 | Grapes | 0
我不能做出Tomato或Grapes成为我的答案,也不能将其作为值 1 仅橙色或第一个添加的数据。
繁星点点滴滴