以下函数允许用户将注释插入名为“comment”的MySQL表中。然后,文件“comments2.php”显示给定提交的所有注释。
用户提交评论后,我希望用户浏览器的顶部以用户刚提交的评论为基础。
我该怎么做?
提前致谢,
约翰
功能:
function show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl)
{
echo '<form action="http://www...com/.../comments/comments2.php" method="post">
<input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
<input type="hidden" value="'.$submissionid.'" name="submissionid">
<input type="hidden" value="'.stripslashes($submission).'" name="submission">
<input type="hidden" value="'.$url.'" name="url">
<input type="hidden" value="'.$submittor.'" name="submittor">
<input type="hidden" value="'.$submissiondate.'" name="submissiondate">
<input type="hidden" value="'.$countcomments.'" name="countcomments">
<input type="hidden" value="'.$dispurl.'" name="dispurl">
<label class="addacomment" for="title">Add a comment:</label>
<textarea class="checkMax" name="comment" type="comment" id="comment" maxlength="1000"></textarea>
<div class="commentsubbutton"><input name="submit" type="submit" value="Submit"></div>
</form>
';
}
文件comments2.php包含:
$query = sprintf("INSERT INTO comment VALUES (NULL, %d, %d, '%s', NULL)", $uid, $subid, $comment);
mysql_query($query) or die(mysql_error());
$submissionid = mysql_real_escape_string($_POST['submissionid']);
$submissionid = mysql_real_escape_string($_GET['submissionid']);
$sqlStr = "SELECT comment.comment, comment.datecommented, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.datecommented ASC
LIMIT 100";
$tzFrom1 = new DateTimeZone('America/New_York');
$tzTo1 = new DateTimeZone('America/Phoenix');
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"commentecho\">";
$count = 1;
while ($row = mysql_fetch_array($result)) {
$dt1 = new DateTime($row["datecommented"], $tzFrom1);
$dt1->setTimezone($tzTo1);
echo '<tr>';
echo '<td rowspan="3" class="commentnamecount">'.$count++.'.</td>';
echo '<td class="commentname2"><a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.$row["username"].'</a></td>';
echo '<td rowspan="3" class="commentname1">'.stripslashes($row["comment"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="commentname2">'.$dt1->format('F j, Y').'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="commentname2a">'.$dt1->format('g:i a').'</td>';
echo '</tr>';
}
echo "</table>";
MySQL表中的字段“comment”:
commentid loginid submissionid comment datecommented
答案 0 :(得分:1)
是。将注释提交到数据库时,请获取存储注释的commentid
。将用户转发至http://whatever/comments_page?query_string#&lt; commentid
&gt;。
然后,在显示评论的页面中,将“id”属性添加到与用户评论对应的行中,其值为commentid
。