简体   繁体   中英

Codeigniter can not delete record

i trying to use codeigniter on my web, i don't know why but i can not delete user from my datase, i think i made a mistake on my code, but i can not find it. can you have a look and help me solve this problem. This is my controller code:

    function user_delete($id_user){
    $this->load->model('membership_model');
    if ($this->membership_model->isAdmin())
    {
        $this->membership_model->deleteUser();
        redirect('/site/admins_area');
    }else
        $this->load->view('not_logged_in_view');
}

And this is my model code:

function deleteUser(){
    $this->db->where('id_user', $this->uri->segment(3));
    $this->db->delete('user');
}

In my view code, when admin click on delete it will guide them to controller like this:

<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal">Delete</button></center>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Modal Header</h4>
    </div>
<div class="modal-body">
      <p>Do you want to delete it?</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <a href="<?php echo base_url();?>site/user_delete" class="btn btn-danger btn-sm" role="button">Delete</a>
    </div>
  </div>

</div>

Can you help?

I think you have a problem with the link

 <a href="<?php echo base_url();?>site/user_delete" class="btn btn-danger btn-sm" role="button">Delete</a>

Use this one:

<a href="<?php echo base_url('site/user_delete');?>" class="btn btn-danger btn-sm" role="button">Delete</a>

If error persist. Please provide the error.

Your Controller

$this->membership_model->deleteUser($id_user);

On Your Model

function deleteUser($id_user){
   $this->db->where('id_user', $id_user);
    $this->db->delete('user');
}

Your code is working, it's just that perhaps you arent linking your routes correctly. You are trying to delete on URL segment, there is no segment 3 as ID.

In order to have the correct URL for this query your URL should look like:

 echo base_url('site/user_delete/'.$user_id);

Assuming you already know how to parse variables to a view, this shouldnt be difficult. Also note, you will need to change your current route which looks likes follow:

 $route['site/user_delete']  = "controllername/function";

to

  $route['site/user_delete/(:num)' = "controllername/function/$1";

This will allow parameters to be send over the URL. Along with that you will need to change your model aswell to accept a parameter instead of direct segment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM