从多个表中回收项目

时间:2015-03-29 04:58:46

标签: php html forms

我正在为已登录的用户创建评论系统。这很简单,我只想要用户的用户名和页面上发布的评论,然后存储在我的user_comments表中。

我有两个数据库表我正在使用user和user_comments。所有用户名信息都存储在我的用户表中。我希望将注释存储到user_comments部分并从那里发布。然后我希望用户的用户名在评论旁边是echod,然后在提交后与user_comments一起存储。我无法弄清楚如何将所有这些同步。截至目前,此帖子和评论存储到我的user_comments中。

如何将用户的用户名发布,然后存储到user_comments表中?

<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...";
?>

0 个答案:

没有答案