Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Pieter Bracke
Pieter Bracke
4,078 Points

Editing user-comments through PHP, problem with displaying correct comment in edit text-area

Hi,

I am currently working on a system where users can comment on my website this is working now I wanted to also add the possibility of users to edit their own posts. I am using a while loop do fetch all the comments the users posted from my database. Here I also added my "EDIT" button when the edit button is clicked a small popup-form shows where the user is able to edit their comment. However I would like to have the comment he is trying to edit to already show in this edit box. I have tried it with a for loop but then I always get the last post in the database instead of the corresponding one where I clicked the edit button. Any helpt would be appreciated :).

<!-- This is the form where users can post their initial comment this is working! -->
<div class="comment-section">

                <form METHOD="POST" action="includes/postcomment.php">
                    <label class="commentlabel">Comment on this article</label>
<?php echo "<input type='hidden' name='date' value= '".date('Y-m-d H:i:s')."' /> "?>
                    <textarea type="text" name="comment" placeholder="write comment" class="commentinput" required ></textarea>
                    <button type="submit" name="commentsubmit" class="comment-btn">Post comment</button>
                </form>

                <div class="user-comments" id="id-user-comments">
<?
/*This is where I connect to my db and echo out all he comments placed by the various users using a while loop. This is also working!*/

                        $sqlgetcomments = "SELECT * FROM comments";
                        $resultgetcomments = mysqli_query($connection, $sqlgetcomments);
                        while($rowcomments = mysqli_fetch_array($resultgetcomments)){

                        echo "<div class='commentdiv'>
                        <p class='commentuser'> Posted by: '".$rowcomments['CommentUsername']."' on:  '".$rowcomments['CommentDate']."'</p>
                        '".$rowcomments['CommentTxt']."'

                            </div>";

/*Here I check if the user is logged in and if the logged in username compares with the user who posted the comment, if this is the case they get to see the EDIT button */
                        if(isset($_SESSION['user']) AND $_SESSION['Username'] == $rowcomments['CommentUsername']){

                            $_SESSION['Usercommentedit'] = $rowcomments['CommentTxt'];
                            $commentdbtxt = $_SESSION['Usercommentedit'];

                            echo "<div class='editcomment'>
                                           /*this button opens the popup window to edit the comment*/
                                        <button onclick='editCommenttoggle()' class='comment-btn-edit'>edit</button>



                                    <form method='POST' action='includes/deletecomment.php' >
                                        <button type='submit' class='comment-btn-edit'>delete</button>
                                    </form>

                                </div>";

                    }else{

                        echo '';
                    }
                }
?>
                </div>

        </div>
<?php 
session_start();
require_once('connect.php');
?>
<!-- This document has the pop up form where users should be able to edit their comment-->
<div class="popupscreen" id="editcommentpopup">
    <div class="formwrapper">
        <div class="login-form">
        <form action="" method="POST">

            <label class="popuplabel"><p>Edit comment</p></label>
            <textarea class="editcommentpopuptxt">
                <?php

                $sqlgetcomments = "SELECT * FROM comments WHERE = CommentTxt = '$' ";
                $resultgetcomments = mysqli_query($connection, $sqlgetcomments);
                if($resultgetcomments){
                /* here I want echo out the comment already posted by the user this is the part which is not working! */
                echo $commentdbtxt;

                }else{
                    echo 'not matching';
                }

            ?>
            </textarea>


            <button class="popupbutton" type="submit"><p>Edit comment</p></button>
        </form>
        <button onclick="closeEditComment()" class="popupbutton"><p>Close</p></button>
    </div>

    </div>
</div>

2 Answers

You could try populating only the comment the user is currently looking at, I believe that way will be a more user-friendly way to edit comments. If you decide to do that, you could use the method the_comment() to call and populate with the current comment content, here is the documentation for it: https://developer.wordpress.org/reference/functions/the_comment/ I have not tried this, so I can't say it will work, but in theory, it should work. Cheers.

Pieter Bracke
Pieter Bracke
4,078 Points

all comments regarding the post show underneath each other on the same page that's why I used a while loop so every comment could be withdrawn from the DB and echo'd out like that. so unfortunately they don't view the comments separately.

Hi Pieter,

I've looked at your posted source code as good as I can. I think your PHP is correct, but your JS/HTML interaction maybe not.

I believe that a look at the HTML/JS in the webbrowser would enlighten us on the problem.

Can I see your code running somewhere (can you post a url to the running website?).

Best regards, Stefan

Pieter Bracke
Pieter Bracke
4,078 Points

Already solved it. simply used some javascript to change the browser url to the normal url + the comment id in the DB. So when the edit button is clicked and then using a GET[''] statement to check which id is in the url will get me the right text linked to the right id in the DB :)