【问题标题】:MySQL Query Issue - PHP variable not passed through includesMySQL 查询问题 - PHP 变量未通过包括
【发布时间】: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 提交其他表单,效果很好。

标签: php mysql


【解决方案1】:

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

【讨论】:

    【解决方案2】:
     <?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";
     }
    
    ?>
    

    【讨论】:

    • 您使 SQL 注入的可执行性更高,并降级了 OP 的数据库驱动程序,为什么?
    • 你怎么能说SQL注入更具可执行性并且降级了OP的数据库驱动程序。
    • 您正在使用mysql_connect。 OP 正在使用mysqli_connect。巨大差距。请参阅警告php.net/manual/en/function.mysql-connect.php。 OP 正在使用mysqli_real_escape_string。您正在将用户数据直接发送到数据库,而无需任何形式的转义,因此用户可以插入他们想要的任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多