php - Codeigniter CRUD app: error updating a record (trying to get property of non-object) -
i have put crud application codeigniter 3. update form has data validation set up, through 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 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 updated'); } else { $this->session->set_flashdata('update-response','failed 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 if, not through 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 problem editing record, inputting invalid data, hitting "save button":
severity: notice message: trying property of non-object filename: views/update.php
such problem appears only on update form. cause of this?
in else replace this:
} 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)); }
Comments
Post a Comment