使用 CodeIgniter 删除数据库中的行


Delete row in database using CodeIgniter

为什么不删除?当我按删除时,我应该得到我要删除的行的 id。

视图:

<?php
foreach ($cust_data as $row) {
    echo '<tr class="even pointer">';
    echo '<td class="a-center ">';
    echo '<input type="checkbox" class="tableflat">';
    echo '</td>';
    echo'<td>' . $row->cust_id . '</td>';
    echo'<td>' . $row->firstname . '</td>';
    echo'<td>' . $row->lastname . '</td>';
    echo'<td>' . $row->email . '</td>';
    echo'<td>' . $row->contact_number . '</td>';
    echo'<td>' . $row->address . '</td>';
    echo'<td>';
    echo '<a href="' . base_url() . 'administrator/delete?id=' . $row->cust_id . '">Delete</a>';
    echo'</td>';
    echo'</tr>';
}
?>    

型:

public function delete($id) {
    $this->db->delete('customer', array('cust_id' => $id));
}

控制器:

public function delete() {
    $this->load->model('admin_model');
    $this->admin_model->delete($this->input->get('cust_id'));
    $this->customer();
}

我注意到你曾经在代码中发布id的值:

echo '<a href="' . base_url() . 'administrator/delete?id=' . $row->cust_id . '">Delete</a>';

在控制器上,您使用了get(),名称为 cust_id

$this->admin_model->delete($this->input->get('cust_id'));

您无法获取任何值,因为cust_id不是已发布值的名称。 因此,请将cust_id更改为id,如下例所示:

public function delete() {
    $this->load->model('admin_model');
    $this->admin_model->delete($this->input->get('id'));
    $this->customer();
}