未使用codeigniter在mysql中插入复选框数据

icnyk63a  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(295)

我尝试在codeigniter中插入我的复选框数据。但数据没有插入数据库。这是我的视图文件:

<input type="checkbox" name="feature[]" value="WIFI" >
  <input type="checkbox" name="feature[]" value="TV">

我试图使用内爆将数组转换为字符串,但是我不知道如何添加$data数组,所以它们一起插入
这是我的控制器:

public function save()
     {
       $this->load->model('Partner_model');
       $feature = $this->input->post('feature');
      $fea=array(
             'feature'=>json_encode(implode(",",$feature)) 
                   );

      $user_data= array(
     'pname' => $this->input->post('pname'),
     'type' => $this->input->post('type'),
     'address' => $this->input->post('address'),
     'about' => $this->input->post('about'),
     'city' => $this->input->post('city'),
      'code' => $this->input->post('code')
     );
    if($this->Partner_model->save($user_data,$fea))
   {
       $msg = "save sucesss" ;
   }
   else
   {
       $msg = "not save";
   }

   $this->session->set_flashdata('msg', $msg);
   $this->load->view('partner_profile');
 }

&这是我的模型:

public function save($data,$fea)
  {
     return $this->db->insert('property', $data,$fea);
  }
93ze6v8z

93ze6v8z1#

你的型号有毛病。
你把三个论点传给 insert() 但是你用的第三个是不合适的。该参数应该是一个布尔值,指示是否转义值和标识符。你需要合并 $fea 进入 $data 这应该在控制器中完成。
有一种更简单的方法来创建数组 $user_data 因为它本质上是 $_POST 只是使用 $this->input->post() .
而且,没有明显的理由说明你为什么要使用 json_encode . 除非您在从db中检索它时需要它,否则没有理由为此而烦恼。考虑删除 json_encode .
首先,改变模式

public function save($data)
{
    return $this->db->insert('property', $data);
}

这里有一个修改过的保存方法

public function save()
{
    $this->load->model('Partner_model');
    $user_data = $this->input->post(); //makes a copy of $_POST
    $feature = $this->input->post('feature');
    if($feature) //because $feature will be null if no boxes are checked
    {
        $user_data['feature'] = json_encode(implode(",", $feature));
    }

    $msg = $this->Partner_model->save($user_data) ? "save sucesss" : "not save";

    $this->session->set_flashdata('msg', $msg);
    $this->load->view('partner_profile');
}

通过评论要求的解释。
打电话给 $this->input->post('pname') 返回的值 $_POST['pname'] 如果存在,则返回null。
当你创建 $user_data 你打了六个电话给 $this->input() 每次用不同的“钥匙”复印一份 $_POST . $this->input->post() 不带任何参数返回整个 $_POST 数组((见文件)

$user_data = $this->input->post();

复制 $_POST 使用一行代码。它将包括 $_POST['feature'] 如果勾选了任何框,但是 $_POST['feature'] 如果未选中任何框,则不会设置。
有两种方法可以测试是否选中了任何框。首先我们可以测试一下 isset($_POST['feature']) == true 或者我们可以测试一下 $this->input->post('feature') == true . 我用第二个打电话

if($feature)

这和下面的任何一行都差不多

if($feature != false)...
   if($feature != null)...
   if( ! empty($feature))...
   if( ! is_null($feature))...

换句话说, if($feature) 计算为true,如果 $feature 一切都准备好了 null , false ,0,“0”,“”(空字符串),array()(空数组)

envsm3lx

envsm3lx2#

public function save()
 {
   $this->load->model('Partner_model');
   $feature = $this->input->post('feature');

   $user_data= array(
     'pname' => $this->input->post('pname'),
     'type' => $this->input->post('type'),
     'address' => $this->input->post('address'),
     'about' => $this->input->post('about'),
     'city' => $this->input->post('city'),
     'code' => $this->input->post('code'),
     'feature'=>json_encode(implode(",",$feature))
  );
  if($this->Partner_model->save($user_data)){
     $msg = "save sucesss" ;
  }else{
     $msg = "not save";
  }
  $this->session->set_flashdata('msg', $msg);
  $this->load->view('partner_profile');
}

模型文件应为:

public function save($data) {
  return $this->db->insert('property', $data);
}

相关问题