Page 1 of 1
Delete posts/comments in Blog
Posted: Thu Jan 29, 2015 2:38 am
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
Re: Delete posts/comments in Blog
Posted: Mon Feb 02, 2015 9:41 pm
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
Re: Delete posts/comments in Blog
Posted: Sat Apr 18, 2015 8:01 pm
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.
Re: Delete posts/comments in Blog
Posted: Sun Apr 19, 2015 1:03 am
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
Re: Delete posts/comments in Blog
Posted: Thu Apr 23, 2015 9:07 pm
by jacek
Glad you got it working, and thanks for sharing the solution