Welcome Guest, Not a member yet? Register   Sign In
Cannot delete from database
#1

(This post was last modified: 07-20-2017, 02:08 PM by ciadmin.)

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
Reply
#2

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

log the id in your Post->delete method. Make sure it's what you expect.
Simpler is always better
Reply
#4

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.
Reply
#5

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>
Reply
#6

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(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.
Reply
#8

(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.
Reply
#9

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.
Reply
#10

(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'
Reply




Theme © iAndrew 2016 - Forum software by © MyBB