更新值0而不是Codeigniter中多个复选框表中的数组

时间:2019-02-06 06:17:44

标签: php codeigniter codeigniter-3

该表单更新数据库中的值0,它应该更新复选框数组,但不起作用。 我已经更新了控制器并查看了,请看一下并检查是否可能。

控制器

function type()
  {
if($this->session->userdata("user_id"))//If already logged in
{
$userID = $this->session->userdata("user_id");
$data['user'] = $this->m_user->getRows($userID);
$data['title'] = 'Type | Categories';
$type=$this->form_validation->set_rules('type[]', 'Type', 'required');

if($this->form_validation->run() === FALSE){
            $data['categories'] = $this->category_model->get_categories();
    $data['main_view']="users/v_type";
    $this->load->view('layouts/main',$data);
    $this->load->view('layouts/sidebar',$data);
    $this->load->view('layouts/footer',$data);
} else {
    // Encrypt password

    $type = $this->input->post('type');
    $this->m_user->type($type);
}else
{
     redirect(base_url().'user/');
}
}

查看

 <form action="<?=  base_url()?>user/type" method="post">
<label><input type="checkbox" value="Textile " name="type[]">Textile </label>
<label><input type="checkbox" value="Textile2 " name="type[]">Textile2 </label>
</form>

型号

public function type(){
            if($this->session->userdata("user_id"))//If already logged in
    {
       $userID = $this->session->userdata("user_id");
      $data['user'] = $this->m_user->getRows($userID);
    $type = $this->input->post('type');
    $data=array('type'=>json_encode(implode(",", $type)),);
        $this->db->where('user_id', $userID);
        return $this->db->update('users', $data);
    }
    }

2 个答案:

答案 0 :(得分:0)

我了解的是,问题出在$ data参数中 $data['user'] = $this->m_user->getRows($userID); //fetching in parameter data $type = $this->input->post('type'); $data=array('type'=>json_encode(implode(",", $type)),); //this should have some column like $data['type'] maybe }

答案 1 :(得分:0)

尝试使用您的更新的模型功能代码,并告诉我您发现了什么

public function type(){
    //If already logged in
    if($this->session->userdata("user_id"))
    {
        $userID = $this->session->userdata("user_id");
        $data['user'] = $this->m_user->getRows($userID);
        $type = $this->input->post('type');
        echo'<pre>';print_r($type);
        $data=array('type'=>json_encode(implode(",", $type)),);
        echo'<pre>';print_r($data);
        $this->db->where('user_id', $userID);
        $flag = $this->db->update('users', $data);
        echo $flag;die;
    }
}