Hello,
I created my blog following your tut; However, How do add the ability to delete a post or comment without going through phpmyadmin.?
Thank you for great great tut's.
blog site: http://bearlyconnected.com/blog/index.php
Delete posts/comments in Blog
Re: Delete posts/comments in Blog
The first thing to do would be to create a function that deletes a post given it's ID
That should give you a nudge in the right direction
function delete_post($post_id){ // stuff! }The basic command you want to use is an SQL DELETE
DELETE FROM `posts` WHERE `post_id` = 10;would remove post 10 for example.
That should give you a nudge in the right direction
Re: Delete posts/comments in Blog
Sorry for the long delay, Life happened...
Anyway, so I got this far hope i did it close anyway (posts.inc.php).
In blog_read.php i have this
Anyway, so I got this far hope i did it close anyway (posts.inc.php).
function delete_post($post_id){ $pid = (int)$post_id; $sql = "DELETE FROM `posts` WHERE `post_id` = {$post_id}"; $post = mysql_query($sql); //$post = mysql_fetch_assoc($post); //$post['comments'] = get_comments($pid); //return $post; //header('location: blog_list.php'); }I know i'm missing something but it works for the most part.
In blog_read.php i have this
<h2><?php echo $post['title']; ?></h2> <h5>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h5> <br /> <p><?php echo $post['body']; ?></p> <br /> <a href="<?php delete_post($_GET['pid']); ?>">Delete</a> <hr />But its executing before anyone even clicks the DELETE link.
Re: Delete posts/comments in Blog
Ok I think I got the link thing figured out by using:
I can delete individual comments now and if i delete a post it deletes all comments if any with it.
I'm somewhat happy with it but if you can look at the mysql code and see if i need to change or add to it.
thank you for your time.
Please try for yourself at http://bearlyconnected.com:81/tuts/blog
function runMyFunction() { delete_comment($_GET['delete']); } if (isset($_GET['delete'])) { runMyFunction(); header("Location: " . $_SERVER["HTTP_REFERER"]); } <a href="blog_read.php?delete=<?php echo $comment['cid']; ?>"><em>Delete</em></a>mysql code:
function delete_comment($post_id){ $pid = (int)$post_id; $sql1 = "DELETE FROM `comments` WHERE `comment_id` = {$pid}"; $post = mysql_query($sql); $post = mysql_query($sql1); //$post = mysql_fetch_assoc($post); //$post['comments'] = get_comments($pid); //return $post; //header('location: blog_list.php'); }I've had to learn and tweak some code her and there. But everything seems to be working now.
I can delete individual comments now and if i delete a post it deletes all comments if any with it.
I'm somewhat happy with it but if you can look at the mysql code and see if i need to change or add to it.
thank you for your time.
Please try for yourself at http://bearlyconnected.com:81/tuts/blog