0

I'm trying to use Codeigniter to update my value once the user had submitted the form. But unfortunately, there was an error of undefined index for few of the variable which I store in an array.

Below is the Code that I use to pass from Controller to the Model

Controller Code :

$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);

Model Code :

   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');
    }

Other $data[] value had no problem but only the 'pregnant' and 'breastfeeding' had the error on Undefined Index. I did try to print_r to show the $data value and all are seem to be alright.

Below are the output :

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

What will be the caused for this 'pregnant' and 'breastfeeding' array? As it keeps getting NULL as a return.

Thanks

9
  • try this in controller 'breastfeeding' => $isBreastfeeding ? $isBreastfeeding : '', and 'pregnant' => $isPregnant ? $isPregnant, : '', Commented Jul 10, 2019 at 12:57
  • $isPregnant and $isBreastfeeding where you defined these two variables? error is not related to your model, as its already defined Commented Jul 10, 2019 at 13:02
  • @bobibobi sorry your method not working Message: syntax error, unexpected ',' Commented Jul 10, 2019 at 13:03
  • @devpro Here $isPregnant = 0; $isBreastfeeding = 0; if($this->input->post('pregnant') == 1){ $isBreastfeeding = 0; $isPregnant = 1; //Is breastfeeding selected } if($this->input->post('breastfeeding') == 1){ $isPregnant = 0; $isBreastfeeding = 1; //Is breastfeeding selected } Commented Jul 10, 2019 at 13:04
  • so if $this->input->post('pregnant') not set? Commented Jul 10, 2019 at 13:05

1 Answer 1

1

You need to use isset() to check either index set or not like

$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 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
@MarcusTan: then you can use $_POST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.