0

I'm practicing with a social network project and I'm having an issue with comment replies. Comments on post are fetching properly. I want to use a modal popup to insert reply to comments into the database. The problem now is that I need to fetch the details of the comment the user want to reply to into my modal popup so I can save it along with the reply content into the database.

But when I click the reply button and the modal pops up, it fetches only the details of the first comment under the post irrespective of the comment I want to reply to.

For example, if I click the reply button on any other comments apart from the first comment, it fetches the details of the first comment instead of the details of the comment I clicked.

How do i make my modal popup to get the details of the comment i want to reply to when i click the reply button of that particular comment.

This is my main comment code below:

<?php 
    $get_comment = "SELECT pstcmt.*, usr.* FROM post_comments pstcmt, users usr WHERE post_id = '$post_id' AND usr.user_id = pstcmt.comment_poster_id ORDER BY comment_date DESC LIMIT 10";
    $get_comment_run = mysqli_query($db_conn, $get_comment);

    while ($comt = mysqli_fetch_assoc($get_comment_run)) 
    {
        $cmtr_fname = $comt['fname'];
        $cmtr_mname = $comt['mname'];
        $cmtr_lname = $comt['lname'];
        $cmtr_username = $comt['username'];
        $cmtr_image = $comt['profile_image'];
        $comment_content = $comt['comment_content'];
        $comment_id = $comt['comment_id'];
        $comment_date = $comt['comment_date'];
        $comment_poster_id = $comt['comment_poster_id'];
        $comment_likes = $comt['likes'];
        $date = date_create($comt['comment_date']);
        $time = strtotime($comt['comment_date']);
        ?>
<div id="main-comment">
    
<div class="we-comment">
    <div class="coment-head">
        <h5><a href="user-profile.php?id=<?=$cmtr_username?>" title=""><?= $cmtr_fname." ".$cmtr_mname." ".$cmtr_lname?></a></h5>
        <span><?= date_format($date, "F j, Y "), "[", date("h:i A", $time),"]"?></span>
        
    </div>
    <p><?=$comment_content?></p>

Here's my modal for comment replies:

<!-- Comment reply Modal -->
                <div class="bs-example">
                 
                    <!-- Modal HTML -->
                    <div id="myModal" class="modal fade" tabindex="-1">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">Reply Comment</h5>
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                </div>
                                <?php 

                                    $bbb = "SELECT * FROM users WHERE user_id = '$comment_poster_id' ";
                                    $bbb_run = mysqli_query($db_conn, $bbb);

                                    foreach ($bbb_run as $get) 
                                        {
                                            $bbb_username = $get['username'];
                                        }
                                ?>
                                <div class="modal-body">
                                    <p style="font-size: 16px; font-weight: 600;">Replying to: <?= $bbb_username//$cmtr_fname." ".$cmtr_lname." - ".date_format($date, "F j, Y ")?></p>
                                    <p style="font-size: 16px; font-weight: 600;"><?= $comment_poster_id?></p>
                                                    <span></span>
                                                    <p style="font-size: 14px;"><?=$comment_content?></p>
                                                    <div class="newpst-input" style="width: 95%;">
                                                        <form method="post" action="operations/insert/comments.php" enctype="multipart/form-data">
                                                            <textarea style="width: 100%;" name="reply_content" placeholder="Beam a response"></textarea>
                                                            <input type="hidden" name="comment_id" value="<?=$comment_id?>">
                                                            <input type="hidden" name="post_id" value="<?=$post_id?>">
                                                            <input type="hidden" name="person_reply" value="<?=$logged_in_user_id?>">
                                                            <div class="attachments">
                                                                <ul>
                                                                    <!-- <li>
                                                                        <i class="fa fa-image"></i>
                                                                        <label class="fileContainer">
                                                                            <input type="file" name="post_image" value=" ">
                                                                        </label>
                                                                    </li> -->
                                                                                                        
                                                                    <li>
                                                                        <button type="submit" name="reply-comment-page" class="cust-btn">Reply</button>
                                                                    </li>
                                                                </ul>
                                                            </div>
                                                        </form>
                                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <!-- Comment reply modal ends here -->

Here's the code to display my replies to comments:

<?php 
        $get_replies = "SELECT cmtrep.*, usr.* FROM comment_replies cmtrep, users usr WHERE comment_id = '$comment_id' AND usr.user_id = cmtrep.person_reply ORDER BY reply_date DESC LIMIT 10";
        $get_replies_run = mysqli_query($db_conn, $get_replies);

        while ($comt_rep = mysqli_fetch_assoc($get_replies_run)) 
        {
            $cmtrep_fname = $comt_rep['fname'];
            $cmtrep_mname = $comt_rep['mname'];
            $cmtrep_lname = $comt_rep['lname'];
            $cmtrep_username = $comt_rep['username'];
            $cmtrep_image = $comt_rep['profile_image'];

            $cmtrep_content = $comt_rep['content'];
            $cmtrep_id = $comt_rep['comment_id'];
            $cmtrep_poster_id = $comt_rep['person_reply'];
            $cmtrep_reply_id = $comt_rep['reply_id'];
            $cmtrep_img = $comt_rep['image'];

            $cmtrep_date = $comt_rep['reply_date'];
            $cmtrep_date = date_create($comt_rep['reply_date']);
            $cmtrep_time = strtotime($comt_rep['reply_date']);
            ?>
<div id="comment-reply">
    
    <div class="we-comment">
        <div class="coment-head">
            <h5><a href="user-profile.php?id=<?=$cmtr_username?>" ><?= $cmtrep_fname." ".$cmtrep_mname." ".$cmtrep_lname?></a></h5>
            <span><?= date_format($cmtrep_date, "F j, Y "), "[", date("h:i A", $cmtrep_time),"]"?></span>
                
        </div>
        <p><?=$cmtrep_content?></p>
        
    </div>
</div><br>

<?php 

} 

?>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Sam Akins
  • 63
  • 6
  • You should read about the importance of prepared statements; your queries are insecure/unstable without them. Please don't say "thanks" in your questions. You are declaring static html element `id`s inside of a loop -- this is a no-no and can lead to unexpected problems. (`id="main-comment"`) Are you generating a single modal? or are you secretly generating multiple modals? Server-side content is going to be executed just once at pageload - `SELECT * FROM users WHERE user_id = '$comment_poster_id'` – mickmackusa Aug 04 '22 at 04:40
  • We might need to close this page with [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429/2943403) or similar. – mickmackusa Aug 04 '22 at 04:47
  • What I want to do is to use a modal to insert replies to comments into the database. The modal contains the form. But the problem I'm having is that the modal only fetch the the details of only the first comment, even when i click the other comments. – Sam Akins Aug 04 '22 at 05:23
  • There will be tens of pre-existing pages on this topic on Stack Overflow. https://stackoverflow.com/q/10626885/2943403 keep researching `pass row data to bootstrap modal onclick` with Google search. All of the information is already on this site. – mickmackusa Aug 04 '22 at 05:28
  • 1
    You are open for [SQL injections](https://stackoverflow.com/questions/601300/what-is-sql-injection) – DarkBee Aug 04 '22 at 06:00

0 Answers0