Welcome Guest, Not a member yet? Register   Sign In
Codeigniter CRUD app error: trying to get property of non-object
#1

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

Code:
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:

Code:
<?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:

Code:
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:

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

There is this problem with editing a record, inputting invalid data, then hitting the "Save button":

Code:
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?


Thank you!
Reply
#2

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

There's no customer object. Looks like you're using an array, so use $customer['id']
Simpler is always better
Reply
#3

(08-22-2017, 12:36 PM)donpwinston Wrote: <?php echo form_open("home/update/{$customer->id}"); ?>

There's no customer object. Looks like you're using an array, so use $customer['id']

As I have written above, it does go through the if, but not the else. So there can not be a problem with $customer->id.
Reply
#4

If you read the documentation it shows the example like below:

PHP Code:
if ($this->form_validation->run() == FALSE)
{
 
   // Above condition is TRUE
}
else
{
 
   // Above condition is FALSE

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB