Page 1 of 1
Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 12:43 pm
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??
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 12:47 pm
by jacek
Try adding
echo mysql_error();
after the mysql_query() line and see if it shows you the problem.
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 1:20 pm
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.
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 3:31 pm
by keithlight
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;
}
?>
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 3:58 pm
by Temor
$post['xxx']
should be
$_POST['xxx']
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 4:37 pm
by keithlight
Tried it but no help bud
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 4:43 pm
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
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 4:52 pm
by keithlight
Thanks Jacek, you mentioned this earlier, where exactly do I need to put this 'error_reporting = E_ALL ' code?
Re: Blog inc Commenting tutorial
Posted: Mon Sep 12, 2011 11:58 pm
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.
Re: Blog inc Commenting tutorial
Posted: Tue Sep 13, 2011 9:06 am
by keithlight
function get_post($pid){
A thousand thanks Jacek
- 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.
Re: Blog inc Commenting tutorial
Posted: Tue Sep 13, 2011 12:19 pm
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
Re: Blog inc Commenting tutorial
Posted: Tue Sep 13, 2011 12:36 pm
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
Re: Blog inc Commenting tutorial
Posted: Wed Sep 14, 2011 6:25 pm
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();
}
?>
Re: Blog inc Commenting tutorial
Posted: Wed Sep 14, 2011 7:47 pm
by jacek
You can use empty() to check if somethign is empty,
Re: Blog inc Commenting tutorial
Posted: Thu Sep 15, 2011 8:55 am
by keithlight
Kinda guessed that, it's the syntax (where to put it and how) to use I'm having a problem with.
Re: Blog inc Commenting tutorial
Posted: Thu Sep 15, 2011 12:21 pm
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.
Re: Blog inc Commenting tutorial
Posted: Thu Sep 15, 2011 3:08 pm
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();
}
Re: Blog inc Commenting tutorial
Posted: Fri Sep 16, 2011 8:16 pm
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){
Re: Blog inc Commenting tutorial
Posted: Sat Sep 17, 2011 2:02 pm
by keithlight
Easy as that !!
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.
Re: Blog inc Commenting tutorial
Posted: Sat Sep 17, 2011 5:21 pm
by jacek
Glad you go it working.