CodeIgniter Forums
Cannot delete from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Cannot delete from database (/showthread.php?tid=68517)

Pages: 1 2


Cannot delete from database - michaeldtrek - 07-20-2017

I cannot delete using a button in a datatabe but I can delete if I use a form and I don't know what I'm doing wrong.

The form that works:

<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<?php echo form_close(); ?>

MY controller:

    public function delete($id){
        $this->post_model->delete_post($id);

        redirect('posts/latest_post');
    }

My model:

public function delete_post($id){

$this->db->where('id', $id);
$this->db->delete('posts');
return true;
}

My button in the datatable:

<td>
       <a class="btn btn-danger" type="button"  href="<?php echo base_url(); ?>posts/delete/<?php echo $post['id']; ?>"><i class="fa fa-close"></i>delete</a>
</td>

Any help will be appreciated

Thanks


RE: Cannot delete from database - cybersven - 07-21-2017

Try like this (model) :
$this->db->delete('posts', array('id' => $id));


RE: Cannot delete from database - donpwinston - 07-21-2017

log the id in your Post->delete method. Make sure it's what you expect.


RE: Cannot delete from database - michaeldtrek - 07-21-2017

Hi,

The Model works and deletes the post if I use it in a from to delete it.  When I add it to a table is when it does not work.  This is how I have the table setup.

<tbody>
<?php foreach($posts as $post) : ?>
    <tr>
        <td><?php echo $post['title']; ?></td>
        <td><?php echo $post['body']; ?></td>
        <td><?php echo $post['created_at']; ?></td>
    <td>
        <a class="btn btn-warning" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>"><i class="fa fa-pencil"></i>Edit</a>
    </td>
NOTE: THE FOLLOWING DOES NOT WORK
    <td>
        <a class="btn btn-danger" type="button"  href="<?php echo base_url(); ?>posts/delete/<?php echo $post['id']; ?>"><i class="fa fa-close"></i>delete</a>
    </td>

    </tr>
<?php endforeach; ?>
</tbody>

Thanks for the help.


RE: Cannot delete from database - Wouter60 - 07-22-2017

Hover your mouse pointer over the button(s), and see which url appears on the status bar of your browser.

Instead of
Code:
<td>
<a class="btn btn-danger" type="button"  href="<?php echo base_url(); ?>posts/delete/<?php echo $post['id']; ?>"><i class="fa fa-close"></i>delete</a>
</td>

Try this:

Code:
<td>
<?= anchor('posts/delete/' . $post['id'],'<i class="fa fa-close"></i>','class="btn btn-danger" type="button"');?>
</td>



RE: Cannot delete from database - InsiteFX - 07-22-2017

Try this:

PHP Code:
<class="btn btn-danger" type="button" href="<?php echo base_url('posts/delete/'.$post['id']); ?>"><class="fa fa-close"></i>delete</a



RE: Cannot delete from database - michaeldtrek - 07-22-2017

(07-22-2017, 02:20 AM)Wouter60 Wrote: Hover your mouse pointer over the button(s), and see which url appears on the status bar of your browser.

Instead of
Code:
<td>
<a class="btn btn-danger" type="button"  href="<?php echo base_url(); ?>posts/delete/<?php echo $post['id']; ?>"><i class="fa fa-close"></i>delete</a>
</td>

Try this:

Code:
<td>
<?= anchor('posts/delete/' . $post['id'],'<i class="fa fa-close"></i>','class="btn btn-danger" type="button"');?>
</td>

 Hi, when I hover over the button I get posts/delete/id   The id number.  I changed the link but it still did not work.


RE: Cannot delete from database - michaeldtrek - 07-22-2017

(07-22-2017, 02:27 AM)InsiteFX Wrote: Try this:

PHP Code:
<class="btn btn-danger" type="button" href="<?php echo base_url('posts/delete/'.$post['id']); ?>"><class="fa fa-close"></i>delete</a

Still didn't work.


RE: Cannot delete from database - Wouter60 - 07-22-2017

Please, define "didn't work".
What happens if you click on the button?
Nothing? Or does it load the requested URL and then nothing happens?
Please, insert the following line of code in the posts/delete function, right after the last this->db-> action:
PHP Code:
echo $this->db->last_query();
die(); 

Pls, tell us what it says.


RE: Cannot delete from database - michaeldtrek - 07-22-2017

(07-22-2017, 09:57 AM)Wouter60 Wrote: Please, define "didn't work".
What happens if you click on the button?
Nothing? Or does it load the requested URL and then nothing happens?
Please, insert the following line of code in the posts/delete function, right after the last this->db-> action:
PHP Code:
echo $this->db->last_query();
die(); 

Pls, tell us what it says.

Hi, and thanks for your time.

When I hover over the button it shows the url localhost/ci-template/posts/delete/1
When I click on the button it redirect back to the page where the table is.
This is what I received after inserting the code you sent me: DELETE FROM `posts` WHERE `id` = '1'