Blog inc Commenting tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Blog inc Commenting tutorial

Post by keithlight »

Getting this:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/html/web554/html/TEST/core/inc/comments.inc.php on line 16

..above comment box when clicking Post title, code as follows:
function get_comments($pid){
	$pid =(int)$pid;
	
	$sql = "SELECT
				`comment_body` AS `body`,
				`comment_user` AS `user`,
				DATE FORMAT(`comment_date`, '%d/%m/%Y %H:%i:%s') AS `date`
			FROM `comments`
			WHERE `post_id` = {$pid}";
			
	$comments = mysql_query($sql);
	
	$return = array();
	while (($row = mysql_fetch_assoc($comments)) !== false){
		$return[] = $row;
	}
	
	return $return;
}
Any ideas??
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

Try adding
echo mysql_error();
after the mysql_query() line and see if it shows you the problem.
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

Thanks Jacek, missed _ from DATE_FORMAT, now I'm looking into why the blogs and comments aren't displaying at all, when I click the blog title in blog_list... just getting "By on (0 comments)" and the comment box at the moment. Fun stuff this php. :D
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

Can't get posts and comments to display on Blog_read page, and I can't see why :oops:

When I click the Post title on Blog_list page all I get is this:

Image

I'm guessing the problem is with Blog_read or Comments.inc code? All's fine with blog_list - titles, text, date, time, and no. of comments all show no problem, database all good. My only changes from tutorials are p's for divs, Any Ideas??

Blog_read
[syntax=php]<?php

include('core/init.inc.php');

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
    if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
	    header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');	
	}
	
	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>Untitled Document</title>
</head>

<body>
<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>
			<h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4>
			
			<hr />
			
			<div><?php echo $post['body']; ?></div>
			
			<hr />
		 
			<?php
			
			foreach ($post['comments'] AS $comment){
			?>
			<h4>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
			<div><?php echo $comment['body']; ?></div>
			<hr />
			<?php
			
			}
			
			?>

<form action="" method="post">
<p>
    <label for="user">Name</label>
    <input type="text" name="user" id="user" />
    </p>
    <p>
    <textarea name="body" rows="20" cols="60"></textarea>
    </p>
    <p>
    <input type="submit" value="Add comment" />
    </p>    
    </form>
	<?php
	}
	?>
	</div>
</body>
</html>
[/syntax]

Comments.inc
<?php
// fetches all of the comments for a given blog post
function get_comments($pid){
	$pid =(int)$pid;
	
	$sql = "SELECT
				`comment_body` AS `body`,
				`comment_user` AS `user`,
				DATE_FORMAT(`comment_date`, '%d/%m/%Y %H:%i:%s') AS `date`
			FROM `comments`
			WHERE `post_id` = {$pid}";
			
	$comments = mysql_query($sql);

	
	$return = array();
	while (($row = mysql_fetch_assoc($comments)) !== false){
		$return[] = $row;
	}

	return $return;
}
// adds a comment
function add_comment($pid, $user, $body){
	if (valid_pid($pid) === false){
		return false;
	}
	$pid   = (int)$pid;
	$user  = mysql_real_escape_string(htmlentities($user));
	$body  = mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO `comments` (`post_id`, `comment_user`, `comment_body`, `comment_date`) VALUES ({$pid}, '{$user}', '{$body}', NOW())");
echo mysql_error();	
	return true;
	
}
?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Blog inc Commenting tutorial

Post by Temor »

$post['xxx'] 
should be
$_POST['xxx']
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

Tried it but no help bud :(
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

Make sure you have error_reporting set to E_ALL it looks like you mistyped a variable name somewhere and the notice messages are an easy way to find where :)
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

Thanks Jacek, you mentioned this earlier, where exactly do I need to put this 'error_reporting = E_ALL ' code? :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

keithlight wrote:Thanks Jacek, you mentioned this earlier, where exactly do I need to put this 'error_reporting = E_ALL ' code? :)
You can edit your php.ini file or add
error_reporting(E_ALL);
in your init file.
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

function get_post($pid){
A thousand thanks Jacek :D - missed out $pid from line 57 in posts.inc file. All working perfectly now. What I would like to do now is seam Blog+Commenting tutorial together with User login+Registration tutorial so the php knows who is posting automatically because they are logged in, so they don't have to fill in their name at the top of the comment box.

Have you covered this in another tutorial?

All the best, K.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

I haven’t covered that exactly but you should be able to work it out form the ones I have done.

Have a go :D
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

OK, I'll give it a good go for sure, the error thingamajig really helps, guess I'll be seeing it a lot ;)
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

HELP !! I need some code to validate the post and comment forms, at the moment I can click the add post/add comment button when the forms are empty, and empty entries are turning up on the pages. I've tried dropping in some code from the registration form validation but it's not working at all!!

Add post:
<?php

include('core/init.inc.php');

if (isset($_POST['user'], $_POST['title'], $_POST['body'])){
    add_post($_POST['user'], $_POST['title'], $_POST['body']);
	header('Location: blog_list.php');
	die();
}

?>
Add comment:
<?php

include('core/init.inc.php');

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
    if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
	    header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');	
	}
	
	die();
}

?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

You can use empty() to check if somethign is empty, 8-)
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

:lol: Kinda guessed that, it's the syntax (where to put it and how) to use I'm having a problem with.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

You could replace
if (isset($_POST['user'], $_POST['title'], $_POST['body'])){
with
if (empty($_POST['user']) === false && empty($_POST['title']) === false && empty($_POST['body']) === false){
since empty() does not care if a variable is defined or not.
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

I was messing about with adding a similar line underneath the original code, didn't occur it could be replaced completely - thanks Jacek, they can't post an empty topic now :) But the add comment code looks a little different with the GET['pid'] and the second 'if' line, I've tried doing the same thing with the 'empty' but it's not happenin' can you put me in the right direction?

Add comment
if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
    if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
	    header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');	
	}
	
	die();
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

For that you only need to check the username and the message really so its the same as before
if (empty($_POST['user']) === false && empty($_POST['body']) === false){
Image
keithlight
Posts: 20
Joined: Fri Sep 09, 2011 9:42 am

Re: Blog inc Commenting tutorial

Post by keithlight »

Easy as that !! 8-) When you know what you're doing :? Thanks Jacek, I'm gonna work my way through all the tutorials, I've leapt on to get a little project done for the wife, she's happy, now I'll go back to basics and hope it all makes a bit more sense.

Happy weekend !! K.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog inc Commenting tutorial

Post by jacek »

:D

Glad you go it working.
Image
Post Reply