你好,所以我正在做这个学校的作业,我有一个评论系统对应的帖子ID,我知道它循环三次,但我给了它的帖子ID。我知道邮政总局一直在变化。我只是不知道如何修复这个bug,有什么想法吗?
<?php require_once("menu.php");
$connection = connectToMySQL();
$selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";
$result = mysqli_query($connection,$selectPostQuery)
or die("Error in the query: ". mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
if (!empty($_POST['comment']) ) #To insert new comments in the database
{
$comment = $_POST['comment'];
$userid = $_SESSION['userID'];
$insertCommentQuery = "INSERT INTO `tblcomments` (`Content`,`UserID`,`PostID`,`Timestamp`) VALUES ('$comment','$userid','$postid',CURRENT_TIMESTAMP)";
$resultComment = mysqli_query($connection, $insertCommentQuery)
or die("Error in the query: ". mysqli_error($connection));
}
echo "<div class='"wrapper'">";
echo "<div class='"titlecontainer'">";
echo "<h1>$row[Title]</h1>";
echo "</div>";
echo "<div class='"textcontainer'">";
echo "<span>$row[Content]</span>";
echo "</div>";
if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
{
?>
<div class="imagecontainer">
<img src="images/<?php echo "$row[ImagePath]"; ?>">
</div>
<?php
}
echo "<div class='"timestampcontainer'">";
echo "<b>Date posted :</b>$row[TimeStamp] ";
echo "<b>Author :</b> Admin";
echo "</div>";
#Selecting comments corresponding to the post
$selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";
$commentResult = mysqli_query($connection,$selectCommentQuery)
or die ("Error in the query: ". mysqli_error($connection));
while ($commentRow = mysqli_fetch_assoc($commentResult))
{
echo "<div class='"commentcontainer'">";
echo "<div class='"commentusername'"><h1>Username :$commentRow[Username]</h1></div>";
echo "<div class='"commentcontent'">$commentRow[Content]</div>";
echo "<div class='"commenttimestamp'">$commentRow[Timestamp]</div>";
echo "</div>";
}
if (!empty($_SESSION['userID']) )
{
echo "<form method='"POST'" action='"'" class='"post-frm'">";
echo "<label>New Comment</label>";
echo "<textarea id='"comment'" name='"comment'"> </textarea>";
echo "<input id='"submit'" type='"submit'" name ='"submit'" class='"button'"/>" ;
echo "</form>";
}
echo "</div>";
echo "<br /> <br /><br />";
}
require_once (footer。php) ?>
嗯,这正是您的脚本所做的。它查询所有帖子,循环遍历它们,然后对所有帖子执行插入操作。要解决这个问题,需要在评论表单中存储帖子的id。当您发布表单时,只需插入一条评论并在表单中使用id。
可以像这样:
<?php
if (array_key_exists('postid', $_POST))
{
$postid = $_POST['postid'];
$comment = $_POST['comment'];
// Perform a single insert here, and use $postid and $comment.
}
// Then, start rendering the page:
require_once(menu.php);
$connection = connectToMySQL();
$selectPostQuery = SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC;
$result = mysqli_query($connection,$selectPostQuery)
or die(Error in the query: . mysqli_error($connection));
while ($row = mysqli_fetch_assoc($result))
{
$postid = $row['ID'];
// Render the post itself here.
?>
<div class="wrapper">;
<div class="titlecontainer">;
<h1><?=$row['Title']?></h1>;
</div>;
<div class="textcontainer">;
<span><?=$row['Content']?></span>;
</div>;
<?php
// Render a comment form for each post (is that what you did?)
if (!empty($_SESSION['userID']) )
{?>
<form method=POST action= class=post-frm>
<label>New Comment</label>
<textarea id=comment name=comment></textarea>
<input type=hidden name=postid value=<?=$postid?>/>
<input id=submit type=submit name =submit class=button/>
</form>
<?}
}
大部分代码都是相同的,只是post数据的处理现在在循环之前完成。
否则,我只是修正了一些小的语法问题(可能引入了新的,我还没有测试它)。
同时,我把HTML从echo中去掉了。当然,这是个人喜好的问题,但经验告诉我,echo语句中的大块HTML不是很好读或可维护的。只需关闭PHP标记,输出原始HTML并只回显其中的变量。您可以使用简短的符号:<?= $value ?>
,它基本上表示<?php echo $value ?>
。