2014-05-17 144 views
-2

你好,我正在做这个学校assigment,我已经做了一个评论系统对应的职位ID,我知道它循环三次,但我给它的职位ID。而且我知道postID一直在改变。我只是不知道如何解决这个错误任何想法?SQL插入查询循环

<?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”)?>

+1

只是指出来,你并没有真正提出过问题。 – myesain

+0

它只是非常尴尬地解释它只是对每个帖子输入评论,而不是我在文本区域输入的评论。 – Steve

回答

0

嗯,这是你的脚本做什么。它查询所有帖子,循环遍历它们,然后为它们执行插入操作。要解决这个问题,请在评论表单中存储帖子的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>  
    <?} 
} 

大部分代码是一样的,只是后期数据的处理,现在在循环之前完成。否则,我只是修复了一些小的语法问题(也许引入了新的,我没有测试过)。

另外,我将HTML取出回声。当然,这是一个有趣的问题,但是经验告诉我,echo语句中的大块HTML是不可读或不可维护的。只需关闭PHP标记,输出原始HTML并仅回显其中的变量。您可以使用简短的表示法:<?= $value ?>,这基本上意味着<?php echo $value ?>

+0

谢谢你,我会测试它,当我回到家,是的,我要解决这个提示和快速反应的thx! – Steve

+0

它的工作非常感谢你<3 – Steve