Search code examples
codeignitersql-deletedelete-row

why delete is successful, but the database is not updated delete?


I have a data erase code structure like this:

CONTROLLER

public function menu_delete($id_menu)
 {

    $menu = $this->menu_model->detail_menu($id_menu);
    $data = array ('id_menu'    => $menu->id_menu);

    $this->menu_model->menu_delete($data);
    $this->session->set_flashdata('msg', 'Data Berhasil Hapus');
    redirect(base_url('superadmin/administrator/menu'),'refresh');
   }

**MODEL **

// Fungsi Detail (Edit) User Controler
public function detail_menu($id_menu) 
{
    $this->db->select('*');
    $this->db->from('menu_user');
    $this->db->where('id_menu',$id_menu);  
    $query = $this->db->get();
    return $query->row(); // fungsi (row) untuk menampilkan single data
    // return $query->result(); // fungsi (row) untuk menampilkan semua data
}


// Delete data
public function menu_delete($data)
{
    $this->db->where('id_menu', $data['id_menu']);
    $this->db->update('menu_user', $data);
}

VIEW

              <button type="button" class="btn btn-sm btn-clean btn-icon btn-icon-md" data-toggle="modal" data-target="#Delete<?php echo $m['id_menu'] ?>"> <i class="la la-trash"></i></a></button></center>


          <!--begin::Modal-->
          <div class="modal fade" id="Delete<?php echo $m['id_menu'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabel">HAPUS DATA INI SEKARANG ?</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  </button>
                </div>
                <div class="modal-body">
                  <h6 style="color: red">PERINGATAN PENTING !!!</h6>
                  <p><b>File yang sudah terhapus, tidak akan bisa dikembalikan lagi.</b><br> Klik <b style="color: blue">"Cancel" </b> untuk membatalkan.</p>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-outline-dark" data-dismiss="modal">Cancel</button>
                  <a href="<?php echo base_url('superadmin/administrator/menu_delete/'.$m['id_menu']) ?>"><button type="button" class="btn btn-danger">Delete</button></a>
                </div>
              </div>
            </div>
          </div>

when a successful notification runs, there are no errors. but the database is not deleted or updated. what's wrong with kenya? Please help


Solution

  • The reason this is happening is that you're updating the record and not deleting it. This is how you'd write a delete query(REFERENCE) -

    Model

    public function menu_delete($data)
    {
        $this->db->where('id_menu', $data['id_menu']);
        $this->db->delete('menu_user');
    
        // Produces:
        // DELETE FROM menu_user
        // WHERE id_menu = $data['id_menu']
    }
    

    If you want to change only one column status from 0 to 1 and vice-versa, @Deepak Singh 's answer should do the deed.

    See if it helps you.