【发布时间】:2020-07-08 04:55:45
【问题描述】:
我将以示例的形式提出我的问题。它是这样的;我有一个 index.php,我所有的帖子都在其中,点击一篇帖子后,我会使用我点击的帖子的 id 来 post.php。在 post.php 中,我有一个可用于所有帖子的评论表单。现在的问题是我想让特定的 cmets 发布到帖子中。我知道我可以通过将来自 index.php 的 post_id 获取到数据库中的评论表来实现这一点。所以请各位大佬,我怎么才能把post_id放到我的评论表里。
这是我的代码
<?php include "includes/db.php";?>
<!-- Receiving My Comment Form -->
<?php
if (isset($_POST['submit_comment'])) {
$name = $_POST['author'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$date = date('Y-m-d h-i-s');
$sql = "INSERT INTO comments (name, email, post, comment, date, status,
post_comment_id) VALUES ('$name', '$email', 'I want to learn PHP',
'$comment', '$date', 'unapprove', '$getcommentid')";
$run = mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CMS SYSTEM</title>
<link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
<script src="../bootstrap/js/bootstrap.js"></script>
<script src="../js/jquery.js"></script>
</head>
<body>
<?php include "includes/header.php"; ?>
<div class="container">
<article class="row">
<section class="col-lg-8">
<!-- Displaying Post coming from Get Super Global from Index.php -->
<?php
if (isset($_GET['post_id'])) {
$sel_sql = "SELECT * FROM posts WHERE id = '$_GET[post_id]'";
$run_sql = mysqli_query($conn, $sel_sql);
while ($rows = mysqli_fetch_assoc($run_sql)) {
echo '
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-header">
<h2>'.$rows['title'].'</h2>
</div>
<div>
<img src="'.$rows['images'].'" width="50%">
<p>'.$rows['description'].'</p>
</div>
</div>
</div>';
}
} else {
echo '<div class="alert alert-danger">No post you selected to show: <a href="index.php">Click Here</a> to see more posts</div>';
}
?>
<hr>
<!-- Displaying Comments Here -->
<h2><u>Comment Section</u></h2>
<?php
if (isset($_GET['post_id'])) {
$com_sql = "SELECT * FROM comments WHERE status = 'approved' AND
post_comment_id = '$_GET[post_id]'";
$run_com = mysqli_query($conn, $com_sql);
while ($result = mysqli_fetch_assoc($run_com)) {
echo '
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-header">
<h2>'.$result['name'].'</h2>
</div>
<div>
<p>'.$result['date'].'</p>
<p>'.$result['comment'].'</p>
</div>
</div>
</div>';
}
} else {
echo '<div class="alert alert-danger">No post you selected to show: <a
href="index.php">Click Here</a> to see more posts</div>';
}
?>
<!-- Comment Form -->
<div class="col-lg-10">
<div class="page-header"><h2>Leave A Comment Here</h2></div>
<div class="container-fluid">
<form class="form-horizontal col-lg-8" action="post.php" method="post">
<div class="form-group">
<label for="name" class="" required>Name</label>
<input id="name" type="text" class="form-control" name="author">
</div>
<div class="form-group">
<label for="email" class="" required>Your Email Address</label>
<input id="email" type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label for="comment" class="">Your Comment Here</label>
<textarea name="comment" id="comment" rows="5" cols="50" tabindex="4" required="required"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-block" name="submit_comment">
</div>
</form>
</div>
</div>
</section>
<?php include 'includes/sidebar.php'; ?>
</article>
</div>
<div style="width:50px; height:50px;"></div>
<?php include 'includes/footer.php'; ?>
</body>
</html>
【问题讨论】:
-
发布您的代码将帮助我们了解您现在的困境,以及我们应该如何帮助您
-
首先,你真的不应该这样编码,你支持“代码注入”,你的页面很容易被黑客入侵,其次,我不确定我是否理解你的数据模型。我假设您在单独的表中有 cmets,因为您希望每个帖子支持多个 cmets,但您没有设置外键(IE post_id),因此获取帖子的 cmets 可能很困难
-
是的,我有单独的评论表,我想要什么我如何获取来自 get url 的 post_id 到 cmets 表
-
是 url "post.php?post_id=1234" 其中 1234 是你的 post_id 吗?
-
尝试在 $com_sql 变量上回显 / var_dump 以查看它在 mysqli_query 之前设置的值,我认为您没有得到正确的值