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

(This post was last modified: 07-22-2017, 11:23 AM by michaeldtrek.)

(07-22-2017, 10:24 AM)[email protected] Wrote:
(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'

I figured it out.  The id that codeigniter is trying to delete doesn't match the id in the database.  Probably from me deleting them from testing.  I need to figure out how to reset the auto increment in the database if I can.  At least I think this is the best way to proceed.

Thanks for you time.
I got it to work, I just dropped the row and re-added it. Also your link that you sent works as well

Thanks to everybody who has chimed in to help me.
Reply
#12

(07-22-2017, 11:11 AM)[email protected] Wrote:
(07-22-2017, 10:24 AM)[email protected] Wrote:
(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'

I figured it out.  The id that codeigniter is trying to delete doesn't match the id in the database.  Probably from me deleting them from testing.  I need to figure out how to reset the auto increment in the database if I can.  At least I think this is the best way to proceed.

$post['id'] <- is not the id from the database? Huh

if you need to reset the id, you do something wrong very wrong
Reply
#13

Quote:I need to figure out how to reset the auto increment in the database if I can.  At least I think this is the best way to proceed.

No it's not.
Let your controller fetch the records (optionally via a model), and pass them as an array to the view that shows the table.
Use a foreach... structure to display the records.

Very basic example:
Code:
<table>
<?php foreach ($posts as $post) : ?>
 <tr><td><?= $post->title;?></td><td><?= anchor('posts/delete/' . $post->id,'Delete');?></td></tr>
<?php endforeach; ?>
</table>

This way, each delete button refers to an existing record.
Reply
#14

(07-23-2017, 04:27 AM)Wouter60 Wrote:
Quote:I need to figure out how to reset the auto increment in the database if I can.  At least I think this is the best way to proceed.

No it's not.
Let your controller fetch the records (optionally via a model), and pass them as an array to the view that shows the table.
Use a foreach... structure to display the records.

Very basic example:
Code:
<table>
<?php foreach ($posts as $post) : ?>
 <tr><td><?= $post->title;?></td><td><?= anchor('posts/delete/' . $post->id,'Delete');?></td></tr>
<?php endforeach; ?>
</table>

This way, each delete button refers to an existing record.

Thanks,  Even though I fixed the issue I'm in the learning mode and I'm going to take your advice and fetch the records in the Model.  For me it's a challenge as I believe it will help me learn how to structure the code better.

Again thanks for your input I really appreciate it.
Reply
#15

Remember to let your model return the records as an array of objects in order to use the object syntax in the view, like $post->id.
Otherwise, you must use the array syntax: $post['id'].

PHP Code:
$query $this->db->order_by('datetime','ASC')->get('posts');
return 
$query->result(); // returns result as an array of objects, use ->result_array() to get an array of arrays 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB