首页 > 解决方案 > 如何只为一篇文章发表评论?php

问题描述

当我为文章发表评论时,评论会在我的应用程序上发布每篇文章。如何只为一篇文章发表评论?

如果您有任何建议或想法,我真的很适用。谢谢!

<!-- Comments Form -->
<?php 
if(isset($_SESSION['user_id'])){
    $users = $db->prepare('SELECT * FROM users WHERE user_id=?');
    $users->execute(array($_SESSION['user_id']));
    $user = $users->fetch();
}

if(isset($_POST['create_comment'])){
    $comment_post_id = $_REQUEST['post_id'];
    
    $comment = $db->prepare('INSERT INTO comments SET comment_user_id=?, comment_post_id=?, comment_contents=?, created_time=NOW()');
    $comment->execute(array(
        
        $user['user_id'],
        $comment_post_id['comment_post_id'],
        $_POST['comment_contents']
        ));
        
    header('Location: post.php');
    exit();
}
?>
            <div class="well">
                <form action="" method="post" role="form">
                <div class="form-group">
                    <label for="Comment">Your Comment</label>
                    <textarea class="form-control" rows="3" name="comment_contents"></textarea>
                </div>
                    <button type="submit" name="create_comment" class="btn btn-primary">Comment</button>
                </form>
            </div>
            <hr>

<!-- Comment -->
<?php 
$comments = $db->query('SELECT u.user_name, c.*, p.* FROM users u, comments c, posts p WHERE u.user_id=c.comment_user_id AND p.post_id=c.comment_post_id ORDER BY c.created_time DESC');
?>
<!-- Comment Preview -->
<h5>Comments</h5>
<hr>
<?php foreach ($comments as $comment): ?>
    <?php $comment['post_id']; ?>
    <p><?php echo htmlspecialchars($comment['comment_contents'], ENT_QUOTES); ?></p>
    <p><?php echo htmlspecialchars($comment['user_name'], ENT_QUOTES); ?>  |  <?php echo htmlspecialchars($comment['created_time'], ENT_QUOTES); ?></p>
    <?php if($_SESSION['user_id'] == $comment['comment_user_id']): ?>
    <a href="delete_comment.php?delete_comment_id=<?php echo htmlspecialchars($comment['comment_id']); ?>">Delete comment</a> 
    <?php endif; ?>
    <hr>
<?php endforeach; ?>
        </div>  
    </div>
</div>
<hr>

评论表 在此处输入图像描述

发布表 在此处输入图像描述

用户表 在此处输入图像描述

标签: phphtmlmysql

解决方案


推荐阅读