MySQL查询问题-PHP变量未通过includes传递


MySQL Query Issue - PHP variable not passed through includes

因此,正如标题所示,我在执行MySQL查询时遇到了问题。查询几乎成功,因为除了一个字段外,所有数据字段都存储在我的数据库中。查询本身是一个评论系统,供登录用户对任何给定的博客文章发表评论。我遇到的问题是无法识别变量"$post_id",因此"$comment_post_id"未存储在我的数据库中。

"$post_id"是在blogs.php中定义的,在回显此变量后,它确实存在并成功定义。但是,该变量不会传递到commentsubmit.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>

commentsubmit.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();
}
?>

您不包括/需要commentsubmit.php脚本中的blogs.php脚本,因此在直接对commentsubmit.php进行POST之后,blogs.php中的代码永远不会运行,除非您在请求最终到达上面commentsubmit.php中显示的代码部分之前,在服务器上自动进行其他请求处理(即服务器端重写或类似处理)。

 <?php
     $con = mysql_connect("localhost","root","password");
     $con=mysql_select_db("database_name");
     error_reporting(0);
     session_start();
if(isset($_POST["submit"])){
     $comment_author = $_POST['full_name'];
     $comment_author_email = $_POST['email'];
     $comment_author_username = $_POST['username'];
    $sql="select * from table_name where `full_name`='"$comment_author "',`email`='"$comment_author_email "',`username`='"$comment_author_username "'";
    $qur=mysql_query($sql);
    $row= mysql_fetch_array($qur);
    $num= mysql_num_rows($qur);
}
 if($num>0){
 $_SESSION["full_name"]=$full_name;
 $_SESSION["email"]=$comment_author_email;
 $_SESSION["username"]=$comment_author_username;
 }
else{
  echo"Username and Password are wrong";
 }
?>