Blog Tutorial - Read Page Problems

Post here is you are having problems with any of the tutorials.
Post Reply
Zoomer
Posts: 5
Joined: Wed Nov 23, 2011 1:54 am

Blog Tutorial - Read Page Problems

Post by Zoomer »

Hello,

I have been following your blog tutorial video's, and everything works fine but the blog_read page. It simply shows By on (0 comments), and the comments form.

I searched your forum, and read another topic with the same problem, but unfotunatly the topic didn't solve mine.

I added error_reporting(E_ALL); to the page and it now reads "Notice: Undefined index" for everything(title, user, date, etc...). As well as " Warning: Invalid argument supplied for foreach() " on line 45.

Im not sure how I fix this.

blog_read.
<?php
error_reporting(E_ALL);

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>BetterPHP - Blog Tutorial</title>
    </head>
    <body>
        <div>
            <?php
                       
	        if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
			    echo 'Invalid Post ID.';
			}else{	
                $post = get_posts($_GET['pid']);
                				
                ?>
				<h2><?php echo $_GET['title']; ?></h2>
				<h4>By <?php echo $_POST['user']; ?> on <?php echo $_POST['date']; ?> (<?php echo count($_POST['comments']); ?> comments)</h4>
				
				<hr />
				
				<p><?php echo $_POST['body']; ?></p>
				
				<hr />
				
				<?php
				
				foreach ($_POST['comments'] as $comment){
                    ?>
                    <hr>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></hr>
					<p><?php echo $comment['body']; ?></p>
                    <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>
Any help would be appreciated.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial - Read Page Problems

Post by jacek »

You use of $_POST here
foreach ($_POST['comments'] as $comment){
does not make much sense. I think that should be one of the return values form the function, somethign like $post['comments'] ?

Also, can you post the code of your get_post function ?
Image
Zoomer
Posts: 5
Joined: Wed Nov 23, 2011 1:54 am

Re: Blog Tutorial - Read Page Problems

Post by Zoomer »

Alright, I changed the foreach line.

Here is my get_post:
//Fetches a single post from the table.
function get_post($pid){
    $pid = (int)$pid;
	
	$sql = "SELECT
	           `post_title` AS `title`,
			   `post_body` AS `body`,
			   `post_user` AS `user`,
			   `post_date` AS `date`
			FROM `posts`
            WHERE `post_id` = {$pid}";
			
    $post = mysql_query(sql);
    $post = mysql_fetch_assoc($post);
	
    $post['comments'] = get_comments($pid);

    return $post;	
	
}

**Edit
I'm rewatching part of the videos, and I think I may have fixed 1 problem.

In blog_read, I had post = get_posts. Added the 's'.

The "Notice: Undefined index" errors are now gone, but now Im getting:

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /comments.inc.php line 17"

comments.inc
//Fetches all comments for a given 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)); 
    $user = 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())");	
	     
	return true;

}

Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Blog Tutorial - Read Page Problems

Post by Tino »

On line 12 you forgot a dollar sign.
Please check out my CodeCanyon items.
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: Blog Tutorial - Read Page Problems

Post by louiegiezer »

You forgot to put a $ sign at line 13 make it $sql...
Zoomer
Posts: 5
Joined: Wed Nov 23, 2011 1:54 am

Re: Blog Tutorial - Read Page Problems

Post by Zoomer »

Alright, with your help I've managed to get the errors to disappear. But I've run into a new problem...

When I post a comment, the comment body is showing up for both username, and body.

e.g.

"By Quick test comment. on 25/11/11 03:11:24

Quick test comment."

Not sure what exactly I changed to cause this.

add_comment
//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)); 
    $user = 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())");	
	     
	return true;

}

Blog_read
<?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>BetterPHP - Blog Tutorial</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 />
				
				<p><?php echo $post['body']; ?></p>
				
				<hr />
				
				<?php
				
				foreach ($post['comments'] as $comment){
                    ?>
                    <hr>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></hr>
					<p><?php echo $comment['body']; ?></p>
                    <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>
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Blog Tutorial - Read Page Problems

Post by Tino »

On line 9 in add_comment.php you need to set the $body variable rather than the $user variable ;)
Please check out my CodeCanyon items.
Zoomer
Posts: 5
Joined: Wed Nov 23, 2011 1:54 am

Re: Blog Tutorial - Read Page Problems

Post by Zoomer »

Everything is working fine now. Thanks for all the help.
Post Reply