Can't get posts and comments to display on Blog_read page, and I can't see why
When I click the Post title on Blog_list page all I get is this:
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;
}
?>