Codeigniter error: updating a record (trying to get property of non-object)

I have put together a CRUD application with Codeigniter 3. The update form has data validation set up, through the controller:

UPDATE function:

public function update($customer_id) {
        // data validation
        $this->form_validation->set_rules('first_name', 'First name', 'required');
        $this->form_validation->set_rules('last_name', 'Last name', 'required');
        $this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

        if ($this->form_validation->run()) {
            $data = [
            // insert into these database table fields
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email' => $this->input->post('email')
            ];
            $this->load->model('Customer');
            if ($this->Customer->updateCustomer($customer_id, $data)) {
                $this->session->set_flashdata('update-response','Customer successfully updated');
            } else {
                $this->session->set_flashdata('update-response','Failed to update customer');
            }
            redirect('home');
        } else {
        $data = [       
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email' => $this->input->post('email'),
            'id' => $customer_id
        ];
        $this->load->view('update', array("customer" => $data));
    }
}
It does go through the if, but not through the else.

UPDATE form:

<?php echo form_open("home/update/{$customer->id}"); ?>

    <div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
        <?php echo form_input('first_name', set_value('first_name', $customer->first_name),[
            'id' => 'first_name',
            'class' => 'form-control'
            ]);
        if(form_error('first_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
        echo form_error('first_name'); ?>                                     
    </div>

    <div class="form-group">
        <?php echo form_submit('submit', 'Save', 'class = "btn btn-success btn-block"'); ?>
    </div>

<?php echo form_close(); ?>

The UPDATE model is:

public function getAllCustomers($customer_id) {
    $query = $this->db->get_where('customers', array('id' => $customer_id));
    if ($query->num_rows() > 0) {
        return $query->row();
    }
}

public function updateCustomer($customer_id, $data) {
    return $this->db->where('id', $customer_id)->update('customers', $data);
}

The UPDATE view:

<?php echo form_open("home/update/{$customer->id}"); ?>
There is this problem with editing a record, inputting invalid data, then hitting the "Save button":

Severity: Notice
Message: Trying to get property of non-object
Filename: views/update.php
Such a problem appears only on the update form. What could be the cause of this?

I believe there is a function similar to get_last_query(). Find the query and also the reason it fails and produces the null value.

It does go through the if, it does not go through the else. :frowning:

Try this:


public function getAllCustomers($customer_id) {
    $query = $this->db->get_where('customers', array('id' => $customer_id));
   echo "<pre>";
     print_r( $query );
   echo '</pre>';
$str = $this->db->last_query();
echo $str; 
   die;
    if ($query->num_rows() > 0) {
        return $query->row();
    }
}

Edit:
Added last_query(…);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.