您好我尝试在codeigniter上进行基本视图编辑和删除
即时通过使用customer / index.php上的表来显示来自db的所有客户数据并且它可以工作,但是当我尝试打开视图函数来查看具有$ id作为参数的特定客户时,仅显示第2个数据,第2个,第3等显示404。
这是我的问题
这是我的数据库结构的ss
customers / index.php
<h2 align="center"><?= $title ?></br></h2>
<table table table-hover width="100%" border="1">
<thead>
<tr class="table-danger">
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Company</th>
<th scope="col">Company Address</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Action</th>
</tr>
</thead>
<?php foreach($customers as $row): ?>
<tbody>
<tr class="table-light">
<th scope="row"><?php echo $row['id']; ?></th>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['company']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td align="justify">
<a class="btn btn-outline-danger" href="<?php echo site_url('/customers/view/'.$row['id']); ?>">Edit/Delete</a>
</p>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p align="center"><a href="<?php echo site_url('/customers/insert');?>"><button type="submit" class="btn btn-danger" >Insert</button></a></p>
控制器/ Customers.php
class Customers extends CI_Controller{
public function index(){
$data['title'] = 'Customers';
$data['customers'] = $this->Cust_model->get_customers();
$this->load->view('templates/header');
$this->load->view('customers/index', $data);
$this->load->view('templates/footer');
}
public function view($id = NULL){
$data['customer'] = $this->Cust_model->get_customers($id);
if(empty($data['customer'])){
show_404();
}
$data['title'] = 'View Customer';
$this->load->view('templates/header');
$this->load->view('customers/view', $data);
$this->load->view('templates/footer');
}
public function insert(){
$data['title'] = 'Add New Customer';
$this->form_validation->set_rules('name','Name', 'required');
$this->form_validation->set_rules('company','Company', 'required');
$this->form_validation->set_rules('address','Address', 'required');
$this->form_validation->set_rules('email','Email', 'required');
$this->form_validation->set_rules('phone','Phone', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('customers/insert', $data);
$this->load->view('templates/footer');
} else {
$this->Cust_model->insert_customer();
redirect('customers');
}
}
public function delete($id){
$this->Cust_model->delete_cust($id);
redirect('customers');
}
public function edit($id = NULL){
$data['customer'] = $this->Cust_model->get_customers($id);
if($id == NULL){
$id = $this->uri-segment(4);
}
$data['title'] = 'Edit Customer';
$this->load->view('templates/header');
$this->load->view('customers/edit', $data);
$this->load->view('templates/footer');
}
模型/ Cust_model.php
<?php
class Cust_model extends CI_Model{
public function _construc(){
$this->load->database();
}
public function get_customers($name = FALSE){
if($name === FALSE){
$query = $this->db->get('cust_table');
return $query->result_array();
}
$query = $this->db->get_where('cust_table', array('name', $name));
return $query->row_array();
}
public function insert_customer(){
$data = array(
'name' => $this->input->post('name'),
'company' => $this->input->post('company'),
'address' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
return $this->db->insert('cust_table', $data);
}
public function delete_cust($id){
$this->db->where('id', $id);
$this->db->delete('cust_table');
return true;
}
public function edit($id, $data){
$this->db->where('id', $id);
$result = $this->db->update('cust_table', $data);
if($result){
return $id;
}else{
return false;
}
}
}
客户/ view.php
<h2 align="center"><?= $title ?></br></h2>
<table table table-hover width="100%" border="1">
<thead>
<tr class="table-danger">
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Company</th>
<th scope="col">Company Address</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr class="table-light">
<th scope="row"><?php echo $customer['id']; ?></th>
<td><?php echo $customer['name']; ?></td>
<td><?php echo $customer['company']; ?></td>
<td><?php echo $customer['address']; ?></td>
<td><?php echo $customer['phone']; ?></td>
<td><?php echo $customer['email']; ?></td>
</tr>
</tbody>
</table>
<p align="center"><br><a class="btn btn-outline-danger" href="<?php echo site_url('/customers/edit/'.$customer['id']); ?>">Edit</a>
<a class="btn btn-danger" href="<?php echo site_url('/customers/delete/'.$customer['id']); ?>">Delete</a>
输入工作正常,当我尝试将按钮删除在index.php上时删除工作正常
请帮助我,谢谢
答案 0 :(得分:0)
当您传递ID时,您需要使用where函数get_customers()
中的id列而不是名称。此外,似乎数组未正确声明应该是array('id' => $id)
而不是array('id', $id)
。
为了清晰起见,我更新了该功能,将$name
变量更改为$id
。
public function get_customers($id = null){
if(is_null($id)){
$query = $this->db->get('cust_table');
return $query->result_array();
}
$query = $this->db->get_where('cust_table', array('id' => $id));
return $query->row_array();
}
我还建议使用flash消息并将用户重定向到索引函数,如果get_customers()
的返回结果为空或者使用show_error('user does not exist', 400)
作为404,则表示用户不存在让某些人感到困惑。