アンケートの 回答をデータベースに入力したい
データベースに次のようなテーブルがあります(例):
| 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"/>