应在生成一行时创建多行

时间:2016-04-14 16:48:06

标签: php mysql

我的社交网站上有评论功能。这个想法是,当用户点击为每条消息打印的comments链接时,会显示现有消息的列表以及添加您自己的评论的表单。即。

enter image description here

但是,当用户添加评论时,它会将评论添加到所有用户帖子。例如Ronda有两个帖子。如果我在Ronda的帖子“Hello”(图片中)中添加评论,那么评论也会显示在id完全不同的帖子中,并且会在我的user_comments表格中生成两行。如果用户有更多帖子,I.e。 Alice有10个帖子,如果我向其中一个帖子添加评论,则会显示所有10个帖子的相同评论(以及db中的10个新行)。

我不明白为什么会这样。以前工作正常,我没有改变任何东西(从我记忆中)。这是我的方法:

<?php
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' ORDER BY id DESC");

while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];
    $message_content = $row['message'];
    $date_of_msg     = $row['post_details'];
    $thoughts_by     = $row['added_by'];
    $attachent       = $row['attachment'];
    $shared          = $row['shared'];

echo "  <div class='message_wrapper'>

        // the actual post will be displayed here, as well as the comment link.

        </div>";

// this is where each comment is displayed 
            echo "<div id='toggleComment$thought_id' class='new_comment'>";
            if ($account_type2 == "user"){
                // displaying the add new comment form ....
                echo "
                    <form action='' method='post' enctype='multipart/form-data'>
                        <table>
                            <tr>
                                <td>
                                    <textarea id='txtarea' name='comment_msg' cols='80' maxlength='180' placeholder=' add your comment...'></textarea> 
                                </td>
                                <td>
                                    <input id='send' type='submit' name='send_comm' value='Share'/> 
                                </td>
                            </tr>
                         </table> <hr/>
                    </form>";
            }
            while($comment = mysqli_fetch_assoc ($get_comm)) { 
                $comment_id = $comment['id'];
                $comment_body = $comment['body_of_msg']; 
                $comment_posted_to = $comment['comment_posted_to']; 
                $comment_posted_by = $comment['comment_posted_by']; 
                $removed = $comment['comment_removed']; 

            $name_q = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$comment_posted_by'");
            while($roww = mysqli_fetch_assoc ($name_q)) {   
                $post_firstname = $roww['first_name'];
                $post_lastname = $roww['last_name'];

            echo " 
            <div class='new_comm' id='thoughtId".$thought_id."-childId".$comment['id']."'> 
                <br/><b><a href = 'profile_page/$comment_posted_by'> $post_firstname $post_lastname</a></b>: $comment_body "; ?><?php 
                } 
            }
            echo "</div>";
/*************************/
    // here is where I process a new comment, and send it to the database.
    // sending comments to database 
    $comment = htmlentities(trim(strip_tags(@$_POST['comment_msg'])));
    $comment = mysqli_real_escape_string($connect, $comment);

    // if button is pressed, do this...
    if(isset($_POST['send_comm'])){
        if (!empty ($comment)){
            $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
            header ("Location: /profile_page/$user");
        } 
    }       
/****************************/
}// while closed

?>

修改

我想我知道问题是什么,但不知道如何解决它。

问题在于if(isset($_POST['send_comm'])){处理是在while循环中完成的,它显示了用户的所有帖子。但是,如果我把它放在while循环之外,那么代码中的$thought_id将是未定义的。我尝试过这种方法:

while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];

    // if button is pressed, do this...
    if(isset($_POST['send_comm'])){
        if (!empty ($comment)){
            $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
            header ("Location: /profile_page/$user");
        } 
    }
}

但是没有评论添加到帖子中(没有生成行,INSERT查询未执行)。

0 个答案:

没有答案