我想将我的问卷 答案输入数据库
我在数据库中有这样的表(示例):
| id | question | answer |
+----+--------------+--------+
| 11 | have dinner? | 1 |
| 12 | have house? | 0 |
| 13 | have garden? | NULL |
| 14 | bla bla bla? | NULL |
// footnote: answer = 0 as no, answer = 1 as yes
问题是如何将我的答案输入到 id 13、14 等中?到数据库
如果我的控制器:
/* I am using CodeIgniter */
$data['form_action'] = site_url('dcm/index');
$answer = $this->input->post('answer');
$id = $this->input->post('id');
$selected_answer = $answer['id'];
$this->dcm_model->inputAnswer($answer, $id); // <-- for input answer to model
我的模型:
function inputAnswer($answer, $id){
$sql = ("
UPDATE question
SET answer = '$answer'
WHERE id = '$id'
"); $this->db->query($sql);
}
在视图中:(我仍然很困惑)
<?php foreach($query->result() as $row) { ?>
<span> <?php echo $row->id . '. ' . $row->question; ?> </span>
<span> <input type="checkbox" value="1" name="answer" /> </span>
<input type="submit" value="submit answer"/>