选择选定的CHECKBOX并将选定的行插入数据库

问题描述 投票:0回答:2

我想将我的问卷答案输入数据库

我在数据库中有这样的表(示例):

| 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"/>
php mysql codeigniter
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);
                }
            }
        }
    }
?>

0
投票

使用像这样的

MVC
图案并使用
update_batch

在控制器中获取表格详细信息

   public function my_function(){       

        $id_array = array();

        $results = $this->my_model->get_results();
        foreach($results as $result)
        {
           array_push($id_array,$result['id']);
        }

        if($this->input->post())
        {
             $checked_id_array = array();
             $checked_ans_Arr = $this->input->post('answer'); 

             $update_array = array();

             foreach($checked_ans_Arr as $checked_ans)
             {
                 $update_array['id'] =  $checked_ans;
                 $update_array['answer'] = 1;
                 array_push($checked_id_array,$checked_ans);
             }

             $not_checked_ids = array_diff($checked_id_array,$id_array);

             if(is_array($not_checked_ids) && count($not_checked_ids )>0)
             {
                 foreach($not_checked_ids as $not_checked_id)
                 {
                    $update_array['id'] =  $not_checked_id;
                    $update_array['answer'] = 0;
                 }
             }

             $this->my_model->update_batch_answers($update_array);

             $results = $this->my_model->get_results();
        } 

        $this->load_view('your_view',array('results'=>$results));    

   }

在模型中

public function get_results()
{
   $this->db->select('*');
   $his->db->from('table');

   $query = $this->db->get();
   return $query->result_array();
}

public function update_batch_answers($update_array)
{
    return $this->db->update_batch('mytable', $update_array, 'id'); 
}

在视野中

<?php  foreach($results as $result) { ?>

<span><?php echo $result['question']; ?></span>
<span> <input <?php if($result['answer']){echo "checked='checked'";} ?> type="checkbox" value="<?php echo $result['id']; ?>" name="answer[]" /> </span>

<?php } ?>

<input type="submit" value="submit answer"/>
© www.soinside.com 2019 - 2024. All rights reserved.