Codeigniter:删除数据

时间:2014-10-03 08:42:28

标签: php codeigniter delete-row codeigniter-url

我的代码有问题,我试图通过codeigniter中的删除按钮从我的数据库中删除一些数据..我的观点:

<a href="<?php echo site_url('admin/m_pakar/delete');?>/<?php echo 
$result['pakar_id'];?>"<i class="fa fa-trash-o fa-fw"></i></a>

我的控制器中的代码:

public function delete() {

    $datasend = array(
            'class'=>'',
            'message'=>'',
        );

    $id = $this->input->post("id", TRUE);
    $this->general->set_table('data_pakar');
    $this->general->where(array('pakar_id' => $id));

    $deletepakar = $this->general->delete();
    if ($deletepakar) {         
        $datasend = array(
                    'class'=>'class="alert alert-success alert-dismissable"',
                    'message'=>'<button type="button" class="close" data-dismiss="alert" 
                    aria-hidden="true">&times;</button>Data berhasil dihapus.',
        );
    } else {

        $datasend = array(
                    'class'=>'class="alert alert-danger alert-dismissable"',
                    'message'=>'<button type="button" class="close" data-dismiss="alert" 
                    aria-hidden="true">&times;</button>Data gagal dihapus.',
        );
    }

    $datasend['pakar'] = $this->general->get_result_array();
    $datasend['level'] = $this->general->get_result_array();

    $dataview['content'] = $this->load->view('admin/m_pakar/index', $datasend, TRUE);
    $this->load->view($this->template, $dataview);

}

这是我的模特:

public function delete() {

    $delete = $this->db->delete($this->tbname);
    return $delete;
}

使用此代码,$deletepakar = $this->general->delete();无效。并且出来的陈述是Data gagal dihapus ...我的代码有什么问题?

2 个答案:

答案 0 :(得分:0)

希望它必须是GET

$id = $this->input->get("id", TRUE);

答案 1 :(得分:0)

尝试

<a href="<?php echo site_url('admin/m_pakar/delete/'.$result['pakar_id']);?>"><i class="fa fa-trash-o fa-fw"></i></a>

控制器: -

public function delete($id) {
 $this->general->where(array('pakar_id' => $id)); // will get you directly from url

更多信息: - Deleting a specific row using codeigniter