我正在尝试编辑记录。 现在在我的按钮中,我将EMP_ID作为uri传递,但它在此基础上进行了id和编辑:
看到这是我的代码:
public function edit($emp_id = NULL)
{
if ($emp_id)
{
$this->data['user'] = $this->user_m->get($emp_id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
echo $emp_id;
// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE)
{
$data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address'));
$data['password'] = $this->user_m->hash($data['password']);
$key=$this->user_m->save($data, $id);
redirect('admin/user/index');
}
}
// Load the view
echo $this->db->last_query();
$this->data['subview'] = 'employee/profile/edit';
$this->load->view('employee/_layout_main', $this->data);
}
这是我的观点:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Employee_Id</th>
<th>Name</th>
<th>Last_Name</th>
<th>Email</th>
<th>Phone</th>
<th>Gender</th>
<th>Designation</th>
<th>User Type</th>
<th>Blood Group</th>
<th>Date of Birth</th>
<th>Status</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php if(count($users)): foreach($users as $user): ?>
<tr class="active">
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->emp_id); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->name); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->last_name); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->email); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->phone); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->gender); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->designation); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->user_type); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->blood_group); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->date_birth); ?></td>
<td><?php echo anchor('admin/user/edit/' . $user->emp_id, $user->status); ?></td>
<td><?php echo btn_edit('employee/profile/edit/' . $user->emp_id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
<?php $this->load->view('admin/components/page_tail') ?>
现在当我点击我的编辑按钮时,网址是正确的。
http://127.0.0.1/project/admin/user/edit/21
但我编辑了id = 21而不是emp_id = 21 的行。
我无法理解我的代码中的错误请帮助
MY_MODEL
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE){
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL){
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
// Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
答案 0 :(得分:1)
请检查控制器中的$ users数组
echo print_r($ users);
它将打印整个用户数组
它可以帮助您跟踪确切的数据
答案 1 :(得分:0)
你可以写这个
<td><?php echo btn_edit('employee/profile/edit/' . $user->emp_id); ?></td>
而不是
<td><?php echo btn_edit('employee/profile/edit/' . $user->id); ?></td>