Mavisa9
Mavisa9

Reputation: 140

i got the error message: Object not found and error 404, when i click Delete Button using PHP and codeigniter

When I click the delete button, the javascript appear, but when I click 'ok', I got the error message.

Here's my code:

The controller file:

public function delete(){
            $this->mpbk_grup->delete($id);
            redirect('cpbk_grup/index');
        }

The model file:

   function delete($ID){
            $this->db->where("ID",  $id);
            $this->db->delete('pbk_groups');
        }

The view file:

<div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th>NO</th>
                    <th>Name</th>
                    <th width="104"></th>
                </tr>
            </thead>
            <?php   $no=1;
                    $n=0;
                    foreach ($pbk_groups as $pbg) { $n++; ?>
                <tr>
                    <td width='10'><?php echo $no++; ?></td>
                    <td><?php echo $pbg->Name; ?></td>
                    <td>
                        <?php echo anchor('cpbk_grup/edit','<i class=fa fa-pencil-square-o"></i> Edit', array('class'=>'btn btn-danger btn-sm"'))?>
                    </td>
                    <td>
                        <a href="javascript:if(confirm('and yakin ingin menghapus??')){document.location='<?php echo base_url();?>cpbk_grup/delete<?php echo $pbg->ID; ?>';}" class="btn btn-danger btn-xs"> Delete</a>
                    </td>
                </tr>
            <?php } ?>


              </table>
            </div>

Hope, you can help me, because I don't know what to do now! :''(

Upvotes: 1

Views: 596

Answers (3)

swapnil kambe
swapnil kambe

Reputation: 123

Controller

public function delete($id){
            $this->mpbk_grup->delete($id);
            redirect('cpbk_grup/index');
        }

View

 <a href="<?php echo base_url();?>admin/delete/<?php echo $pbg->ID; ?>"  onclick="return confirm('and yakin ingin menghapus??');"class="btn btn-danger btn-xs"> Delete</a>

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

1st : URL value should be enclosed by single quotes or double quotes

<a href="javascript:if(confirm('and yakin ingin menghapus??')){ document.location="<?php echo base_url();?>cpbk_grup/delete/<?php echo $pbg->ID; ?>"; }" class="btn btn-danger btn-xs"> Delete</a>

2nd : you missed (/) slash before the id

document.location="<?php echo base_url();?>cpbk_grup/delete/<?php echo $pbg->ID; ?>";

3rd : you need to get the id value from uri parameter like below

Controller: Don't forgot to load the url helper .

public function delete(){

        $id=$this->uri->segment(3);

        $this->mpbk_grup->delete($id);
        redirect('cpbk_grup/index');
    }

Model : your getting the value in model in uppercase but in query using lowercase variables are case sensitive take care about that

     function delete($ID){
            $this->db->where("ID",  $ID);
            $this->db->delete('pbk_groups');
        }

Load the modal in autoload

If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the model to the autoload array.

$autoload['model'] = array('mpbk_grup');

Upvotes: 1

user7596840
user7596840

Reputation: 137

Are you getting id on controller that you want to delete get it from url like this on controller

$id=$this->uri->segment(4);
$this->mpbk_grup->delete($id);
redirect('cpbk_grup/index');

Upvotes: 0

Related Questions