0

アンケートの 回答をデータベースに入力したい

データベースに次のようなテーブルがあります(例):

| 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"/>
4

2 に答える 2

1

データベースのデータを更新したいですか? これを試して:

ビューで:

<?php  
echo form_open('controller/method');
foreach($query->result() as $row) { ?>
<span> <?php echo $row->id . '. ' . $row->question; ?> </span>
<span> <input type="checkbox" value="<?=$row->id?>" name="answer[]" /> </span>
<?php } ?>
<input type="submit" value="submit answer"/>
</form>

コントローラーで:

<?php
    $this->dcm_model->inputAnswer();
?>  

モデル内:

<?php
    function inputAnswer(){
        $data   = array();
        if($this->input->post('answer')){
            $ans    = $this->input->post('answer');
            foreach($ans as $each){
                if(isset($each) && $each != ""){
                    $data['answer'] = '1';
                    $this->db->where('id', $each)->update('questionnaire', $data);
                }
            }
        }
    }
?>
于 2013-08-16T10:03:18.827 に答える