Hello.
I am using a book called Blog Design Solutions by Andy Budd et al to build my own blog (chapter 7). The site is running on localhost and pretty much everything works ... except the 'add a comment' system. A form in the sidebar of the 'posts' page invites users to add comments. When the form is filled in and the 'post comment' button is hit, the machine whirrs through its paces briefly then presents a page with sidebars correctly formatted but with a bit of a mess in place of the expected content:
1 - the post has disappeared
2 - three error messages , each reading:
Notice: Undefined index: myposts (one reads mycomments) in /Library/WebServer/Documents/test/post.php on line (number)
The code for and following each of the salient line numbers reads:
<?php
if ($myposts) {
$sql = "SELECT comment_id, name, website, comment FROM comments WHERE post_id = $post_id";
$result3 = mysql_query($sql);
$mycomments = mysql_fetch_array($result3);
}
?>
:
<?php
if($myposts) {
do {
$post_id =$myposts["post_id"];
$title = $myposts["title"];
$post = format($myposts["post"]);
$dateattime = $myposts["dateattime"];
echo "<h3>$title</h3>\n";
//echo "<h4>posted on $dateattime</h4>\n";
echo "<div class='post'>\n $post \n</div>";
echo "<div class='right'>posted on $dateattime</div>";
} while ($myposts = mysql_fetch_array($result));
} else {
echo"<p>there is no post matching a post_id of $post_id.</p>";
}
?>
:
<?php
if($mycomments) {
echo "<dl>";
do {
$comment_id = $mycomments["comment_id"];
$name = $mycomments["name"];
$website = $mycomments["website"];
$comment = format($mycomments["comment"]);
if ($website !="") {
echo "<dt><a href='$website'>$name</a> wrote:</dt>\n";
} else {
echo "<dt>$name wrote:</dt>\n";
}
echo "<p><dd>$comment</dd></p>\n";
} while ($mycomments = mysql_fetch_array($result3));
echo "</dl>";
} else {
echo"<p class='central'>use the add a comment form to leave a comment.</p>";
}
?>
All the php tags are present. I see from checking in phpMyAdmin that the comments reach the database.
I'm a bit flumoxed on this one and knowing what 'undefined index' means might help me to define it.
I run a Mac iBook G4 on OSX 10.4.10 using php 5.2.0, mySql 5.0.24a.
Any help would be greatly appreciated.
Many thanks
David