【发布时间】:2015-12-02 16:58:20
【问题描述】:
正如标题所示,我在执行 MySQL 查询时遇到了麻烦。查询工作几乎成功,因为所有数据字段都存储在我的数据库中,除了一个。查询本身是一个评论系统,供登录用户对任何给定的博客文章发表评论。我遇到的问题是无法识别变量“$post_id”,因此“$comment_post_ID”未存储在我的数据库中。
'$post_id' 是在 blogs.php 中定义的,并且在回显这个变量之后它确实存在并且被成功定义。但是,此变量不会传递到 cmetsubmit.php,它包含在定义变量的同一文件中。为什么会这样?
这是我的所有代码:
blogs.php(显示所有用户的所有帖子,如果在 url 中设置了 ?id,则只显示一篇帖子。如果设置了 ?id,用户可以评论他们正在查看的单个帖子。)
<?php
if (isset($_GET['id'])) {
$conn = mysqli_connect("localhost", "root", "mypassword", "mydbname");
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$post_id = mysqli_real_escape_string($conn, $_GET['id']);
$blog_post = "SELECT * FROM blogs WHERE id = '$post_id'";
$blog_query = mysqli_query($conn, $blog_post);
while ($row = mysqli_fetch_array($blog_query)) {
$title = $row['title'];
$body = $row['body'];
$author = $row['author'];
$author_username = $row['author_username'];
$datetime = time_ago($row['datetime'], $granularity=2);
}
include ("./fullpageblog.php");
if (isset($_SESSION['id'])) {
include ("./blogcomment.php");
include ("./commentsubmit.php");
}
echo "$post_id";
mysqli_close($conn);
}
?>
blogcomment.php(用户发表评论的表单)
<div class="row col-sm-12">
<div id="fullPageBlog">
<div id="center-border"></div>
<form action="commentsubmit.php" method="post">
<textarea maxlength="1000" id="blogComment" name="content" placeholder="Write your response..."></textarea>
<input type="submit" name="comment" value="Publish" />
</form>
<script type="text/javascript">$('#blogPost').elastic();</script>
</div>
</div>
cmetsubmit.php(评论查询本身)
<?php
session_start();
if (isset($_POST['comment'])) {
$conn = mysqli_connect("localhost", "root", "mypassword", "mydbname");
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$comment_post_ID = $post_id;
$comment_author = $_SESSION['full_name'];
$comment_author_email = $_SESSION['email'];
$comment_author_username = $_SESSION['username'];
$comment_date = date("Y-m-d H:i:s");
$comment_content = mysqli_real_escape_string($conn, $_POST['content']);
$user_ID = $_SESSION['id'];
$comment_submit = "INSERT INTO comments (comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_username, comment_date, comment_content, user_ID) VALUES ('', '$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_username', '$comment_date', '$comment_content', '$user_ID') ";
$comment_query = mysqli_query($conn, $comment_submit);
mysqli_close($conn);
header("Location: blogs.php");
die();
}
?>
【问题讨论】:
-
$post_id 未在您的 cmetsubmit.php 文件中定义,您应该在生成该视图时从 $_POST 或通过添加隐藏字段或将其附加到类似
?post_id=$post_id的 url 中获取该视图. -
什么comment_postID?你可以
$comment_post_id = $post_id,但永远不要在任何地方定义$post_id。所以你实际上是在做$comment_post_id = null。请注意,您很容易受到sql injection attacks 的攻击。你并没有逃避你应该做的一切。 -
$post_id 在 blogs.php 中定义,我尝试在 cmetsubmit.php 中使用同一行代码再次定义它: $post_id = mysqli_real_escape_string($conn, $_GET['id']) ;但这不起作用。
-
因为您在 cmetssubmit 中使用
POST而不是GET进行处理;你也没有我可以在你的表单中看到的名为id的元素。 -
我不明白为什么我会使用 GET 而不是 POST?我在这个项目的不同领域使用 POST 提交其他表单,效果很好。