Page 1 of 1

Blog Tutorial Help with Deleting Posts and Comments on page

Posted: Fri Nov 04, 2011 2:32 pm
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!

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

Posted: Sat Nov 05, 2011 7:49 pm
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

[syntax=xhtml]<a href="?action=delete&amp;comment_id=7">Delete</a>[/syntax]
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

[syntax=php]<?php

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

?>[/syntax]

The delete_comment() function could look something like this.

[syntax=php]function delete_comment($comment_id){
$comment_id = (int)$comment_id;

mysql_query("DELETE FROM `comments` WHERE `comment_id` = {$comment_id}");
}[/syntax]

Does that help :D ?

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

Posted: Sun Nov 06, 2011 9:26 pm
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!

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

Posted: Sun Nov 06, 2011 9:40 pm
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
[syntax=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>
[/syntax]

and my function:
[syntax=php]// 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;
}[/syntax]

it won't update into the db. Any thoughts?

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

Posted: Mon Nov 07, 2011 11:57 pm
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.

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

Posted: Wed Nov 09, 2011 3:07 am
by kgdd
I removed the result and the post title and post body portion you stated and still no luck.

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

Posted: Wed Nov 09, 2011 6:28 pm
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

[syntax=php]echo mysql_error();[/syntax]
after the query and it might tell you why it is failing.

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

Posted: Sat Nov 26, 2011 5:12 pm
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?

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

Posted: Sat Nov 26, 2011 5:20 pm
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

[syntax=php]header('Location: index.php');[/syntax]
for example.