简体   繁体   中英

How to remove a record by id in Codeigniter

I am creating a function to remove a record in Codeigniter, but it is not working properly.

This is my buttons on properties_list

 <th>
  <div><a title="Delete" class="delete btn btn-sm btn-danger pull-right '.$disabled.'" data-href="'.base_url('admin/properties/del/'.$row['id']).'" data-toggle="modal" data-target="#confirm-delete"> <i class="material-icons">delete</i></a></div>
</th>

And my function DELETE on my controller Properties_php

  public function del($id = 0){
   $this->db->delete('ci_properties', array('propertie_id' => $id));
   $this->session->set_flashdata('msg', 'Imóvel removido!');
   redirect(base_url('admin/properties'));
 }

在此处输入图片说明

Try this:

<th>
   <div>
      <a title="Delete" 
         class="delete btn btn-sm btn-danger pull-right <?=$disabled?>" 
         data-href="<?=site_url('admin/properties/del/'.$row['id'])?>" 
         data-toggle="modal" 
         data-target="#confirm-delete">
         <i class="material-icons">delete</i>
      </a>
   </div>
</th>

尝试这个:

<a title="Delete" class="delete btn btn-sm btn-danger pull-right '.$disabled.'" data-href="<?php echo base_url('admin/properties/del/'.$row['id']);?>" data-toggle="modal" data-target="#confirm-delete"> <i class="material-icons">delete</i></a>

Try this:

<a href="<?php 
     echo base_url(); 
         ?>/admin/properties/del/<?php 
     echo $row['id']; 
         ?>" type="button" class="btn btn-danger" style="margin-left: 5px;">Delete</a>

define a var like $url then place it in the href tag

 <th>
     <div>
     <?php $url = base_url('admin/properties/del/'.$row['id']);?>
      <a title="Delete" class="delete btn btn-sm btn-danger pull-right '.$disabled.'" 
       data-href="<?=$url?>" data-toggle="modal" data-target="#confirm-delete"> <i 
       class="material-icons">delete</i></a>
    </div>
  </th>
<th>
  <div><a title="Delete" class="delete btn btn-sm btn-danger pull-right '.$disabled.'" data-href="<?php echo base_url();?>admin/properties/del/<?php echo $row['id']; ?>" data-toggle="modal" data-target="#confirm-delete"> <i class="material-icons">delete</i></a></div>
</th>

if this gives you an error, maybe your controller name doesn't match with link in data-href.

Try the Below code delete

function fnDelete(id)
{
$.ajax({
url: "<?php echo site_url('admin/properties/del'); ?>",  
method: 'POST',
data: { Autoid: id },    
success:function(result) {

   window.location.href= "<?php echo site_url('admin/properties'); ?>";
}
});
}

 public function del(){
$id=$this->input->post('Autoid');
    $this->db->delete('ci_properties', array('propertie_id' => $id));
         $this->session->set_flashdata('msg', 'Imóvel removido!');
    redirect(base_url('admin/properties'));
  }

In your initial request you are sending unprocessed PHP as the url.

use

<?= $variable;?>
<?= function();?> 

to process the link contents in the php view.

我这样解决了

 <div><a title="Delete" class="delete btn btn-sm btn-danger pull-right '.$disabled.'" data-href="<?php echo base_url('admin/properties/del/'.$properties['propertie_id']);?>" data-toggle="modal" data-target="#confirm-delete"> <i class="material-icons">delete</i></a></div> 

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