Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选中选中复选框并将其插入数据库的选定行中_Mysql_Codeigniter - Fatal编程技术网

Mysql 选中选中复选框并将其插入数据库的选定行中

Mysql 选中选中复选框并将其插入数据库的选定行中,mysql,codeigniter,Mysql,Codeigniter,我想把我的调查问卷答案输入数据库 我在数据库中有这样的表(示例): 问题是如何将我的答案输入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 =

我想把我的调查问卷答案输入数据库

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

问题是如何将我的答案输入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
<?php
    $this->dcm_model->inputAnswer();
?>  
观点:(我仍然感到困惑)


是否要更新数据库中的数据?试试这个:

鉴于:

<?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>


像这样使用
MVC
模式并使用
update\u 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"/>


我使用过这个,但它不会改变我数据库表中的任何内容。。。就是这样吗?“$this->dcm_model->inputswer();”??
<?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);
                }
            }
        }
    }
?>
   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"/>