php - 如何只为一篇文章发表评论?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>
解决方案
推荐阅读
- python - Python转义符号中的串行读取
- python - Python groupby itertools 方法
- angular - 确保最后一个 http 帖子在 Angular 中最后到达后端
- sql - 仅从新数据库中获取新数据并插入旧数据库
- firebase - React Native (Expo) Firebase Phone Auth with invisible reCaptcha
- reactjs - Preact + Jest:“moduleNameMapper”和“moduleDirectories”配置未注册
- c - Xcode:未定义符号:_omp_get_max_threads
- angular - 没有为 window.open 实现玩笑
- python - sqlite3 在查询时正在更改列的值
- amazon-web-services - 如何将资源输出(EC2 实例 ID)作为环境变量传递给 Terraform 中的 Lambda 函数?