Приложение Codeigniter CRUD: ошибка обновления записи (попытка получить свойство необъекта)

Я собрал приложение CRUD с Codeigniter 3. В форме обновления настроена проверка данных через контроллер:

ОБНОВИТЬ функция:

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

Это делает пройти через если, но не через остальное.

ОБНОВИТЬ форма:

<?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(); ?>

Обновление модель является:

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

Обновление Посмотреть:

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

Существует проблема с редактированием записи, вводом неверных данных и нажатием кнопки «Сохранить»:

Severity: Notice
Message: Trying to get property of non-object
Filename: views/update.php

Такая проблема появляется только в форме обновления. Что может быть причиной этого?

-1

Решение

в другом случае замените этим:

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

Другие решения

Вам придется пройти объект $ customer из вашей функции обновления в контроллере. Так что ваша другая часть должна выглядеть как

$customer = $this->Customer->getCustomer($customer_id);
$this->load->view('update',$customer);
0

Вы не передали данные для просмотра, поэтому вы получаете предупреждение

$this->load->view('update');

к

$this->load->view('update', array('customer'=>$your_array));

Будет примерно так

$this->load->view('update',array(
'customer'=>$this->Customer->updateCustomer($customer_id, $data)
)
);
0

Пожалуйста, отправьте данные в режиме загрузки как …
например

$data['customer'] =  $this->Customer->updateCustomer($customer_id, $data);

$this->load->view('update',$data);

А потом получить данные как $customer->id так далее..

Вы не можете передавать данные, поэтому была показана ошибка

Попытка получить свойство необъекта

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector