Delete posts/comments in Blog

Post here is you are having problems with any of the tutorials.
Post Reply
jarrodatt
Posts: 13
Joined: Wed Sep 26, 2012 12:21 am

Delete posts/comments in Blog

Post by jarrodatt »

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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Delete posts/comments in Blog

Post by jacek »

The first thing to do would be to create a function that deletes a post given it's ID
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 :)
Image
jarrodatt
Posts: 13
Joined: Wed Sep 26, 2012 12:21 am

Re: Delete posts/comments in Blog

Post by jarrodatt »

Sorry for the long delay, Life happened...

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.
jarrodatt
Posts: 13
Joined: Wed Sep 26, 2012 12:21 am

Re: Delete posts/comments in Blog

Post by jarrodatt »

Ok I think I got the link thing figured out by using:
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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Delete posts/comments in Blog

Post by jacek »

Glad you got it working, and thanks for sharing the solution :)
Image
Post Reply