Hello jeremyphphaven
Your offer is much appreciated.
Latest code as follows
above DTD:
<?php
//open connection to database universal
include("../db_connect.php");
//get post_id from query string universal
$post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";
//if post_id is a number get post from database
if (preg_match("/^[0-9]+$/", $post_id)) {
$sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') AS dateattime FROM posts
WHERE post_id=$post_id LIMIT 1";
$result = mysql_query($sql);
$myposts = mysql_fetch_array($result);
}
// universal
include("functions.php");
//if comment has been submitted and post exists then add comment to database
if (isset($_POST["postcomment"]) != "") {
$posttitle = addslashes(trim(strip_tags($_POST["posttitle"])));
$name = addslashes(trim(strip_tags($_POST["name"])));
$email = addslashes(trim(strip_tags($_POST["email"])));
$website = addslashes(trim(strip_tags($_POST["website"])));
$comment = addslashes(trim(strip_tags($_POST["comment"])));
$sql = "INSERT INTO comments
(post_id,name,email,website,comment)
VALUES ('$post_id', '$name', '$email', '$website', '$comment')";
$result2 = mysql_query($sql);
if (!$result2) {
$message = "failed to insert comment.";
} else {
$message = "comment added.";
$comment_id = mysql_insert_id();
//send yourself an email when a comment is successfully added
$emailsubject = "comment added to: ".$posttitle;
$emailbody ="comment on '".$posttitle."'"."\r\n"
."http://www.thewrightline.co.uk/post.php?post_id=".$post_id ."#c".$comment_id."\r\n\r\n"
.$comment."\r\n\r\n"
.$name." (".$website.")\r\n\r\n";
$emailbody = stripslashes($emailbody);
$emailheader = "from: ".$name." <".$email.">\r\n"."reply-to: ".$email;
@mail("david@thewrightline.com", $emailsubject, $emailbody, $emailheader);
//direct to post page to eliminate repeat posts
header("Location: post.php?post_id=$post_id&message=$message");
}
}
//pull comments from database
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);
}
?>
in sidebar (form to complete for posting a comment):
<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="post_id" value="<?=$post_id ?>" />
<input type="hidden" name="posttitle" value="<?=$title ?>" />
<h4>add a comment:</h4>
<form action="<? /twlcouk/post.php?>" method="post">
<input type="hidden" name="post_id" value="<?=$post_id ?>" />
<input type="hidden" name="posttitle" value="<?=$title ?>" />
<?php
if (isset($message)) {
echo "<p>".$_POST["message"]."</p>";
}
?>
<p>name: <br /><input name="name" type="text" /></p>
<p>email: <br /><input name="email" type="text" /></p>
<p>website: <br /><input name="website" type="text" /></p>
<p>comment: <br /><textarea name="comment" cols="20" rows="10"></textarea></p>
<p class="align"><input type="submit" name="postcomment" value="post comment" /></p>
</form>
in main body of page:
<?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>";
}
?>//above code dumps post on page - it works
<h4>comments</h4>
<div id="comments">
<div class="link">
//following code should dump comment posted from sidebar form at
//<p class='central'...</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>";
}
?>
When comments form is completed and submit button hit, the following happens:
1
Notice: Undefined variable: myposts in /Library/WebServer/Documents/twlcouk/post.php on line 55
is inserted above the top banner
lines 55-59 read:
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);
}
2
text of post is replaced by:
Notice: Undefined variable: myposts in /Library/WebServer/Documents/twlcouk/post.php on line 136
there is no post matching a post_id of .
lines 136-149 read:
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>";
}
3
The following appears where the new comment is expected:
Notice: Undefined variable: mycomments in /Library/WebServer/Documents/twlcouk/post.php on line 158
use the add a comment form to leave a comment.
lines 158-175 read:
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>";
}
The table comments on database david reads (eg):
comment_id 2
post_id 0
name david wright
email davidwright@xxxx.com
website www.xxxxx.com
comment test
tstamp 2007-07-30 21:48:33
All post_id entries are 0
Sorry it's such a long post.
Hope it's all relevant. Please let me know if you need further info.
And thanks again for your interest.
Cheers
David