Blog Tutorial Help with Deleting Posts and Comments on page

Ask about a PHP problem here.
Post Reply
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Blog Tutorial Help with Deleting Posts and Comments on page

Post by kgdd »

Just wanted to say great tutorials, I have used quite a few of yours and they rock!

I am trying to figure out how to add a feature to the blog you built in the Blog tutorial. These features are in the admin section and I was wondering how I would go about writing the PHP and MySQL to have a link on each comment (visible only in the admin section, I have already taken care of restricting the page to admins), and a delete link for each post created, again this is only visible in the admin section of the blog site and I have already taken care of that. I just need the php and mysql code for making the delete link so it deletes the appropriate comment and/or post out of the db. Is this possible? I haven't been able to get it to work.

Please let me know if there is a tutorial for this on your site or already been asked in another post.

Thanks for any help in advance!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by jacek »

Well if you have a separate admin area you can add the delete link only there :s

You could link to the current page with an action set, for example
<a href="?action=delete&comment_id=7">Delete</a>
The id would have to be generated dynamically which should be pretty simple since you should already have the id from the function.

Then at the top of the page you can check for this with php
<?php

if (isset($_GET['action'])){
    if ($_GET['action'] == 'delete'){
        delete_comment($_GET['comment_id']);
    }
}

?>
The delete_comment() function could look something like this.
function delete_comment($comment_id){
    $comment_id = (int)$comment_id;

    mysql_query("DELETE FROM `comments` WHERE `comment_id` = {$comment_id}");
}
Does that help :D ?
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by kgdd »

Yes yes yes, that did work indeed!! However, it is saying Invalid post id when the page reloads. Where would I put in a go back function so it automatically goes back to list the comments with the post? By the way your tutorials rock and are easy to understand!
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by kgdd »

Would you be able to help me with another issue? I'm trying to figure out how to add an edit blog page.

Here is my blog_edit.php
<?php
include('core/init.inc.php');


if (isset($_GET['pid'], $_POST['title'], $_POST['body'])){



die();

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Magicfish Blog Edit</title>
</head>

<body>
<a href="index.php">Admin Home</a>
    <div>
        <?php

if (isset($_GET['pid']) ===false || valid_pid($_GET['pid']) === false){
echo 'Invalid post ID';
}else{
$post = get_post($_GET['pid']);

        ?>

<h2><?php echo $post['title']; ?></h2>


<hr />

<p><?php echo $post['body']; ?></p>

<hr />



        <form action="" method="post">
             <p>
<label for="title">Title</label>
            <input type="text" name="title" id="title" value="<?php echo $post['title']; ?>"/>
            </p>
            <p>
                <textarea name="body" rows="20" cols="60"><?php echo $post['body']; ?></textarea>
            </p>
            <p>
                <input type="submit" value="Edit Post" />
            </p>
        </form>
<?php
}
?>
    </div>
</body>
</html>
and my function:
// edits a blog entry
function edit_post($pid, $title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{$body}' WHERE `post_id` = {$pid}");

echo $result;
}
it won't update into the db. Any thoughts?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by jacek »

You don't want the "(`post_title`, `post_body`)" part in the SQL.

Also the echo $result does not really make sense since $result is never defined.
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by kgdd »

I removed the result and the post title and post body portion you stated and still no luck.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by jacek »

kgdd wrote:I removed the result and the post title and post body portion you stated and still no luck.
Post your code as it is now. Also you can try adding
echo mysql_error();
after the query and it might tell you why it is failing.
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by kgdd »

For the comment delete option above, how would I make the page automatically go back to the previous pid page or make it refresh just once?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial Help with Deleting Posts and Comments on p

Post by jacek »

kgdd wrote:For the comment delete option above, how would I make the page automatically go back to the previous pid page or make it refresh just once?
Not entirely sure what you mean really, you can redirect the browser using the Location header
header('Location: index.php');
for example.
Image
Post Reply