1

I'm creating a commenting system for signed in users. It is very simple, I just want the user's username and the comment posted on the page and then stored in my user_comments table.

I have two db tables I am working with users and user_comments. All of the username info is stored in my users table. I want the comments to be stored into the user_comments section and post from there. Then I want the user's username to be echod next to the comment and then stored in with the user_comments after submitted. I can't figure out how to sync all of this together. As of now this posts and the comment stores into my user_comments.

How would I get the user's username to post and then be stored into the user_comments table?

<form action="communitySignedIn.php" method="POST">
    <label for="comment">Comment</label>
    <textarea cols="15" name="comment" placeholder="Message" class="messageinput" rows="5" maxlength="1000" required></textarea><br>

    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
    <input id="signinButton" name="submit" type="submit" value="Comment">
</form><br><br>

<?php
    if(isset($_POST['submit'])){ 
        $comment = trim( $_POST['comment'] );; 
        $okay = true;    
        if( strlen($comment) < 2 ) { 
            echo "Please enter another character<br>"; 
            $okay = false; 
        } 
        // you would do other validation here if other fields 
        if ( $okay ) { 
            $con = mysqli_connect("localhost", "root", "", "bfb"); 
            $q = mysqli_query($con,"INSERT INTO user_comments (id, comment) VALUES ('', '$comment')") 
                or die("Could not INSERT: " . mysql_error()); 
            echo "Your comment was successfully posted.<br/ ><br /><br /><br />"; 
        } 
    }
?>
<?php
    $con = mysqli_connect("localhost", "root", "", "bfb");
    $run = mysqli_query($con,"SELECT * FROM user_comments ORDER BY id DESC");
    $numrows = mysqli_num_rows($run);
    if($numrows > 0){
        while($row = mysqli_fetch_assoc($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo 'escape($user->data()->username)';
            echo "Comment: $dbcomment<br><br><hr>";
        }
    }
    else
        echo "No Comments Found...";
?>
Choxx
  • 945
  • 1
  • 24
  • 46
  • *"I can't figure out how to sync all of this together"* after dropping a longer description of a whole program is not a good fit for a programming Q&A site. In which sense is your question different from existing Q&A material on how about creating a comment system with PHP and mysql that is on site already? Please make that clear and provide references how existing material couldn't help you and write why. – hakre Mar 29 '15 at 07:31
  • I closed against the reference question which technically explain why your code can't work in a stable manner (at least for the database interaction). You most likely have more issues than the database interaction, so the duplicate will not answer your whole question. For more answers please do a search on PHP, Mysql and Commenting System. – hakre Mar 29 '15 at 07:34

0 Answers0