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

Php 阵列上未定义索引的Codeigniter模型,php,codeigniter,Php,Codeigniter,我试图在用户提交表单后使用Codeigniter更新我的值。但不幸的是,对于我存储在数组中的少数变量,存在未定义索引的错误 下面是我用来从控制器传递到模型的代码 控制器代码: $prep_data = array( 'full_name' => $this->input->post('full_name'), 'nric' => $this->input->post('nric'),

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

下面是我用来从控制器传递到模型的代码

控制器代码:

$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[]值没有问题,但只有“怀孕”和“母乳喂养”在未定义的索引上有错误。我确实试着打印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

谢谢

您需要使用来检查索引集或不喜欢

$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 
}

在控制器“母乳喂养”中尝试此项=>$Is母乳喂养$母乳喂养:''和“怀孕”=>$isPregnant$isPregnant,:'',
$isPregnant
$IsMirropping
您在哪里定义了这两个变量?错误与您的模型无关,因为它已经存在defined@bobibobi很抱歉,您的方法无法工作消息:语法错误,意外“,”@devpro Here$isPregnant=0;$Is母乳喂养=0;如果($this->input->post('pregned')==1){$Is母乳喂养=0;$isPregnant=1;//是否选择母乳喂养}在表达式的结果上(您可以改为使用“null!==expression”)@MarcusTan:然后您可以使用
$\u POST