I have a project on codeigniter. I want to delete the data from database. if user clicks a delete button it should be deleted.
my problem
I think all code is working ..but I think id is not getting in my controller
there is no error, but data is also not deleting from database
offerfetch.php(view)
<td><a type="button" class="btn btn-danger" href="<?= base_url('admin/delarticles'); ?>/<?php echo $emp->offer_id; ?>">Delete</a></td> <td><a type="button" class="btn btn-info" href="<?= base_url('admin/editoffer'); ?>/<?php echo $emp->offer_id; ?>">update</a></td>
admin.php(controller)
public function delarticles() { $id=$this->input->get('offer_id'); $this->load->model('adminloginmodel'); if($this->adminloginmodel->del($id)) { $this->session->set_flashdata('insertsuccess','Delete Successfully'); $this->session->set_flashdata('msg_class','alert-success'); } else { $this->session->set_flashdata('insertsuccess','Please try again..not delete'); $this->session->set_flashdata('msg_class','alert-danger'); } return redirect('admin/viewoffers'); }
in order to debug I have used
print_r($id); exit();
after the line $id=$this->input->get('offer_id');
in a controller but I’m not getting any id, blank page is coming up.
adminloginmodel.php(model)
public function del($id) { return $this->db->delete('offers',['offer_id'=>$id]); }
Advertisement
Answer
You are sending the id through the url of your anchor element:
<a type="button" class="btn btn-danger" href="<?= base_url('admin/delarticles'); ?>/<?php echo $emp->offer_id; ?>">Delete</a>
which should generate something like:
href="your_app/admin/delarticles/1"
you are trying to read the id as it was coming from an input field from a form, that’s not the case here, you can access it directly in your function from url like
public function delarticles($id=0){ if ($id){ $this->load->model('adminloginmodel'); if($this->adminloginmodel->del($id)) { $this->session->set_flashdata('insertsuccess','Delete Successfully'); $this->session->set_flashdata('msg_class','alert-success'); } else { $this->session->set_flashdata('insertsuccess','Please try again..not delete'); $this->session->set_flashdata('msg_class','alert-danger'); } return redirect('admin/viewoffers'); }else{ // your code in case there is no value for $id, aka $id=0 } }
Resuming:
if you use a form and a submit button within this form, you create a $_POST array from your form’s input fields (e.g.
<input type="text" name="offer_id">
, which you can read in the controller with$id=$this->input->get('offer_id')
;use an anchor element and send the parameter in the Url