Codeigniter CRUD 应用程序:更新记录时出错(尝试获取非对象的 属性)
Codeigniter CRUD app: error updating a record (trying to get property of non-object)
我用 Codeigniter 3 组合了一个 CRUD 应用程序。更新表单通过控制器设置了数据验证:
更新功能:
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));
}
}
它通过 if,但不通过 else。
更新表格:
<?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}"); ?>
编辑记录时出现此问题,输入无效数据,然后点击 "Save button":
Severity: Notice
Message: Trying to get property of non-object
Filename: views/update.php
这样的问题只出现在更新表单上。这可能是什么原因?
您没有将数据传递到您的视图,因此您收到警告
$this->load->view('update');
到
$this->load->view('update', array('customer'=>$your_array));
会是这样的
$this->load->view('update',array(
'customer'=>$this->Customer->updateCustomer($customer_id, $data)
)
);
请在加载视图中发送数据,例如...
例如
$data['customer'] = $this->Customer->updateCustomer($customer_id, $data);
$this->load->view('update',$data);
然后获取 $customer->id
等数据..
您无法传递数据,这就是显示错误的原因
Trying to get property of non-object
您必须从控制器中的更新功能传递 $customer 对象。所以你的其他部分应该看起来像-
$customer = $this->Customer->getCustomer($customer_id);
$this->load->view('update',$customer);
在你的 else 中替换为:
} 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));
}
我用 Codeigniter 3 组合了一个 CRUD 应用程序。更新表单通过控制器设置了数据验证:
更新功能:
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));
}
}
它通过 if,但不通过 else。
更新表格:
<?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}"); ?>
编辑记录时出现此问题,输入无效数据,然后点击 "Save button":
Severity: Notice
Message: Trying to get property of non-object
Filename: views/update.php
这样的问题只出现在更新表单上。这可能是什么原因?
您没有将数据传递到您的视图,因此您收到警告
$this->load->view('update');
到
$this->load->view('update', array('customer'=>$your_array));
会是这样的
$this->load->view('update',array(
'customer'=>$this->Customer->updateCustomer($customer_id, $data)
)
);
请在加载视图中发送数据,例如... 例如
$data['customer'] = $this->Customer->updateCustomer($customer_id, $data);
$this->load->view('update',$data);
然后获取 $customer->id
等数据..
您无法传递数据,这就是显示错误的原因
Trying to get property of non-object
您必须从控制器中的更新功能传递 $customer 对象。所以你的其他部分应该看起来像-
$customer = $this->Customer->getCustomer($customer_id);
$this->load->view('update',$customer);
在你的 else 中替换为:
} 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));
}