2014-10-03 81 views
0

非常感谢您提供的所有帮助和建议。下面是我的回答我的问题:使用codeigniter删除特定行

视图

<td><a href ="<?php echo site_url('helloworld/delete/'.$row->user_no);?>">delete</a></td> 

控制器

function delete($user_no) { 
    $this->load->model("dbmodel"); 
    $this->dbmodel->delete_row($user_no); 

} 

模式

public function delete_row($id){ 
    $this -> db -> where('user_no', $id); 
    $this -> db -> delete('users'); 
    redirect('helloworld/'); 
} 

希望这可以帮助你:)


我是新的codeigniter。我试图删除特定行,但我总是得到这样的错误:

404 Page Not Found

The page you requested was not found.

这里是我的代码在我观点

<td><?php echo anchor('helloworld/delete_row?id='.$row->user_no, 'DELETE', 'id="$row->user_no"'); ?></td> 

模式

function row_delete($id) {  
    $this->db->where('user_no', $id); 
    $this->db->delete('users'); 
} 

控制器

function delete_row(){ 
    $id = $this->input->get('id'); 
    $this->load->model('dbmodel'); 
    $this->dbmodel->row_delete($id); 
} 
+0

您可以指定重现你得到,例如错误所需的步骤它的制作网址是? – kix 2014-10-03 06:11:03

+0

我有这个在我的网址:http://localhost/ci/index.php/helloworld/delete_row/id/5 它可以得到我想要删除的id,但它说我请求的页面是未找到。 – 2014-10-03 06:30:10

回答

2

尝试直接添加锚点

<a href ="<?php echo site_url('controller/delete/'.$row->user_no);?>">delete</a> 
or 
<?php echo anchor('controller/delete/'.$row->user_no, 'Delete','title="delete"');?> 

模式功能: -

public function deleteRecord($table, $where = array()) { 
    $this->db->where($where); 
    $res = $this->db->delete($table); 
    if($res) 
    return TRUE; 
    else 
    return FALSE; 
} 

控制器: -

public function delete($id = '') { 
    $this->load->model('dbmodel'); 
    $where = array('user_no' => $id); 
    $this->dbmodel->deleteRecord('table_name',$where); 
} 
+0

未定义的变量id – 2014-10-03 07:06:32

+0

需要发送$ row-> user_no而不是$ id检查更新 – 2014-10-03 07:10:16

+0

意味着你不存在id你存在的地方你调用锚点 – 2014-10-03 07:19:36

0

试试这个而产生的锚标记: -

<?php echo anchor('helloworld/delete_row/id/'.$row->user_no, 'DELETE', "id='".$row->user_no."'"); ?> 

在这里,我假设的HelloWorld将是你的控制器的名称。

+0

是的,helloworld是我的控制器。我尝试了你的建议,但仍然有同样的错误。 我有这个在我的网址:http://localhost/ci/index.php/helloworld/delete_row/id/5 – 2014-10-03 06:26:18

+0

确保您的控制器名称以大写字母开头,像这样的公共类Helloworld – 2014-10-03 06:31:59

+0

是的,我的控制器启动用大写字母。虽然同样的错误。 – 2014-10-03 06:36:55

0

试试这个:

查看

<td><?php echo anchor('helloworld/delete_row/'.$row->user_no, 'DELETE', 'id="$row->user_no"'); ?></td> 

控制器

function delete_row($id){ 
    $this->load->model('dbmodel'); 
    $this->dbmodel->row_delete($id); 
} 
+0

我试过你的建议先生,但我仍然得到同样的错误。 – 2014-10-03 07:03:06

+0

你的控制器在这个路径上工作:http://localhost/ci/index.php/helloworld – 2014-10-03 07:07:17

+0

localhost/ci/index.php/helloworld是正确的先生 – 2014-10-03 07:09:17