阵列上的Codeigniter模型未定义索引

时间:2019-07-10 12:48:57

标签: php codeigniter

用户提交表单后,我正尝试使用Codeigniter更新我的值。但是不幸的是,对于我存储在数组中的几个变量,存在未定义索引的错误。

下面是我用来从Controller传递到Model的代码

控制器代码:

$prep_data = array(
                'full_name' => $this->input->post('full_name'),
                'nric' => $this->input->post('nric'),
                'dob' => $this->input->post('dob'),
                'mobile_phone' => $this->input->post('mobile_phone'),
                'email' => $this->input->post('email'),
                'address_line_1' => $this->input->post('address_line_1'),
                'address_line_2' => $this->input->post('address_line_2'),
                'postcode' => $this->input->post('postcode'),
                'state' => $this->input->post('state'),
                'current_brand' => $this->input->post('current_brand'),
                'pregnant' => $isPregnant, //Edit by Marcus
                'breastfeeding' => $isBreastfeeding, //Added by Marcus
                'child_full_name' => $this->input->post('child_full_name'),
                'child_dob' => $this->input->post('child_dob'),
                'child_current_brand' => $this->input->post('child_current_brand'),
                'expected_give_birth_date' => $this->input->post('expected_give_birth_date'),
                'image' => '',
                'quantity_purchase' => $this->input->post('quantity_purchase'),
                'foc_amount' => $this->input->post('foc_amount'),
                'marketing_opt' => $this->input->post('marketing_opt'),
                'centre_id' => get_cookie('abbott_centre', TRUE),
                'user_id' => $this->session->id,
                'sampling_day' => $centre_data['sampling_day'],
                'created_at' => $datetime,
                'updated_at' => $datetime
            );
$this->entry_model->update_entry($prep_data);

型号代码:

   function update_entry($data){
        echo print_r($data);
        if(!empty($data['full_name'])) $this->db->set('full_name', $data['full_name']);
        if(!empty($data['nric'])) $this->db->set('nric', $data['nric']);
        if(!empty($data['dob'])) $this->db->set('dob', $data['dob']);
        if(!empty($data['mobile_phone'])) $this->db->set('mobile_phone', $data['mobile_phone']);
        if(!empty($data['email'])) $this->db->set('email', $data['email']);
        if(!empty($data['address_line_1'])) $this->db->set('address_line_1', $data['address_line_1']);
        if(!empty($data['address_line_2'])) $this->db->set('address_line_2', $data['address_line_2']);
        if(!empty($data['postcode'])) $this->db->set('postcode', $data['postcode']);
        if(!empty($data['state'])) $this->db->set('state', $data['state']);
        if(!empty($data['current_brand'])) $this->db->set('current_brand', $data['current_brand']);
        if(!empty($data['pregnant'])){ //Fix by Marcus
            $this->db->set('pregnant', $data['pregnant']);
        }else {
            $this->db->set('pregnant', 0);
        }
        if(!empty($data['breastfeeding'])){ //Fix by Marcus
            $this->db->set('breastfeeding', $data['breastfeeding']);
        }
        else {
            $this->db->set('breastfeeding', 0);
        }
        if(!empty($data['expected_give_birth_date'])) $this->db->set('expected_give_birth_date', $data['expected_give_birth_date']);//Fix by Marcus
        if(!empty($data['child_full_name'])) $this->db->set('child_full_name', $data['child_full_name']);
        if(!empty($data['child_dob'])) $this->db->set('child_dob', $data['child_dob']);
        if(!empty($data['child_current_brand'])) $this->db->set('child_current_brand', $data['child_current_brand']);
        if(!empty($data['image'])) $this->db->set('image', $data['image']);
        if(isset($data['quantity_purchase'])) $this->db->set('quantity_purchase', $data['quantity_purchase']);
        if(isset($data['foc_amount'])) $this->db->set('foc_amount', $data['foc_amount']);
        if(!empty($data['marketing_opt'])) $this->db->set('marketing_opt', $data['marketing_opt']);
        if(!empty($data['centre_id'])) $this->db->set('centre_id', $data['centre_id']);
        if(!empty($data['user_id'])) $this->db->set('user_id', $data['user_id']);
        if(!empty($data['created_at'])) $this->db->set('created_at', $data['created_at']);
        if(!empty($data['updated_at'])) $this->db->set('updated_at', $data['updated_at']);

        $this->db->where('id', $data['id']);
        $this->db->update('entry');
    }

其他$ data []值没有问题,但是只有“怀孕”和“母乳喂养”在未定义索引上有错误。我确实尝试过print_r以显示$ data值,而且一切似乎都还不错。

下面是输出:

Array ( [full_name] => e2e [nric] => 123 [dob] => 2019-07-09 [mobile_phone] => 1231231231 [email] => [address_line_1] => [address_line_2] => [postcode] => [state] => [current_brand] => [pregnant] => 0 [breastfeeding] => 1 [child_full_name] => [child_dob] => 0000-00-00 [child_current_brand] => [expected_give_birth_date] => 2019-07-09 [image] => [quantity_purchase] => 123123 [foc_amount] => 123 [sampling_day] => [updated_at] => 2019-07-10 19:27:43 [id] => 8 ) 1Array ( [image] => 8_20190710_192743.jpg [id] => 8 ) 1

此“怀孕”和“母乳喂养”阵列会是什么原因?因为它不断得到NULL作为返回。

谢谢

1 个答案:

答案 0 :(得分:1)

您需要使用isset()检查索引集或不喜欢

$isPregnant = 0; 
$isBreastfeeding = 0; 
if(isset($_POST['pregnant']) && $_POST['pregnant'] == 1)
{ 
    $isBreastfeeding = 0; 
    $isPregnant = 1; //Is breastfeeding selected    
} 

if(isset($_POST['breastfeeding']) && $_POST['breastfeeding'] == 1)
{ 
    $isPregnant = 0; 
    $isBreastfeeding = 1; //Is breastfeeding selected 
}