Deleting a specific row using codeigniter

2019-06-08 19:43发布

问题:

Thank you for all the help and suggestions. Here's my answer to my problem:

view

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

controller

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

}

model

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

I hope this can help you :)


I am new in codeigniter. I'm trying to delete a specific row but I always get this error:

404 Page Not Found

The page you requested was not found.

Here is my code in my view:

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

model:

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

controller:

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

回答1:

try to add directly an anchor

<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"');?>

model function :-

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

controller :-

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


回答2:

Try this while generating the anchor tag:-

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

Here, I am assuming that helloworld will be the name of your controller.



回答3:

Try this one:

View

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

Controller

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