php 使用 get 值的问题。适用于页面的一部分但不适用于另一部分

php忍者

我正在研究我的 crud 技能,但我遇到了一些麻烦。第一页显示所有博客文章的列表,当用户点击阅读更多时,它通过 url 发送特定的文章 ID,并由使用该 ID 显示单个文章的目标页面接收/这里是该部分的代码. 它实际上工作正常

function display_single_post($title,$author,$date,$image,$content){
                        $main_page_blog_html  =         "<h2>
                                                            <a href='#'>%s</a>
                                                        </h2>
                                                        <p class='lead'>
                                                            by <a href='index.php'>%s</a>
                                                        </p>
                                                        <p><span class='glyphicon glyphicon-time'></span> Posted on %s</p>
                                                        <hr>
                                                        <img class='img-responsive' src='images/%s'>
                                                        <hr>
                                                        <p>%s</p>

                                                        <hr>
                                                        <hr>";

                            printf("{$main_page_blog_html}",$title,$author,$date,$image,$content);


if(isset($_GET["id"])){
                        $id = $_GET["id"];

                        $stmt = $connect->link->query("SELECT * FROM posts WHERE post_id = $id");
                        while($row = $stmt->fetch()){

                            $post_title = $row["post_title"];
                            $post_author = $row["post_author"];
                            $post_date = date('F j, Y \a\t g:ia', strtotime( $row["post_date"] ));
                            $post_image = $row["post_image"];
                            $post_content = $row["post_content"];
                            $id = $row["post_id"];

                            display_single_post($post_title,$post_author,$post_date,$post_image,$post_content);

                        }
                    }

就像我说的,这一切正常。收到 get 值并加载帖子。问题是当我尝试在查询中使用 $_get id 插入评论时。所有这些代码都在一个页面上,我只显示没有 html 的 php。无论如何,这是插入评论的代码

if(isset($_POST["create_comment"])){
                        global $connect;

                        $post_id = $_GET["id"];

                        $comment_author = $_POST["comment_author"];
                        $author_email = $_POST["author_email"];
                        $comment_content = $_POST["comment_content"];
                        $comment_status = "pending";

使用所有代码进行编辑

    <div class="container">

        <div class="row">

            <!-- Blog Entries Column -->
            <div class="col-md-8">

                <h1 class="page-header">
                    Page Heading
                    <small>Secondary Text</small>
                </h1>

                <!-- First Blog Post -->
                <?php 

                   $connect = new db();

                    if(isset($_POST["create_comment"])){
                        global $connect;
                        echo "hello";
                        $post_id = $_GET["id"];

                        $comment_author = $_POST["comment_author"];
                        $author_email = $_POST["author_email"];
                        $comment_content = $_POST["comment_content"];
                        $comment_status = "pending";

                        $sql = "INSERT INTO comments(comment_post_id, comment_author, comment_email, comment_content, comment_status)
                                VALUES(:a,:b,:c,:d,:e)";
                        $stmt = $connect->link->prepare($sql);
                        $stmt->bindvalue(":a",$post_id);
                        $stmt->bindvalue(":b", $comment_author);
                        $stmt->bindvalue(":c",$author_email);
                        $stmt->bindvalue(":d",$comment_content);
                        $stmt->bindvalue(":e",$comment_status);
                        $stmt->execute();
                    }




                    function display_single_post($title,$author,$date,$image,$content){
                        $main_page_blog_html  =         "<h2>
                                                            <a href='#'>%s</a>
                                                        </h2>
                                                        <p class='lead'>
                                                            by <a href='index.php'>%s</a>
                                                        </p>
                                                        <p><span class='glyphicon glyphicon-time'></span> Posted on %s</p>
                                                        <hr>
                                                        <img class='img-responsive' src='images/%s'>
                                                        <hr>
                                                        <p>%s</p>

                                                        <hr>
                                                        <hr>";

                            printf("{$main_page_blog_html}",$title,$author,$date,$image,$content);

                    }






                    if(isset($_GET["id"])){
                        $id = $_GET["id"];

                        $stmt = $connect->link->query("SELECT * FROM posts WHERE post_id = $id");
                        while($row = $stmt->fetch()){

                            $post_title = $row["post_title"];
                            $post_author = $row["post_author"];
                            $post_date = date('F j, Y \a\t g:ia', strtotime( $row["post_date"] ));
                            $post_image = $row["post_image"];
                            $post_content = $row["post_content"];
                            $id = $row["post_id"];

                            display_single_post($post_title,$post_author,$post_date,$post_image,$post_content);

                        }
                    }

                ?>
                <hr>

                <!-- Blog Comments -->

                <!-- Comments Form -->
                <div class="well">
                    <h4>Leave a Comment:</h4>
                    <form role="form" method="post" action="post.php">
                         <div class="form-group">
                            <input type="text" class="form-control" name="comment_author" placeholder="name">
                        </div>
                        <div class="form-group">
                            <input type="email" class="form-control" name="author_email" placeholder="email">
                        </div>
                        <div class="form-group">
                            <textarea class="form-control" rows="3" name="comment_content"></textarea>
                        </div>
                        <button type="submit" name="create_comment" class="btn btn-primary">Submit</button>
                    </form>
                </div>

                <hr>

                <!-- Posted Comments -->

                <!-- Comment -->
                <div class="media">
                    <a class="pull-left" href="#">
                        <img class="media-object" src="http://placehold.it/64x64" alt="">
                    </a>
                    <div class="media-body">
                        <h4 class="media-heading">Start Bootstrap
                            <small>August 25, 2014 at 9:30 PM</small>
                        </h4>
                        Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                    </div>
                </div>

                <!-- Comment -->
                <div class="media">
                    <a class="pull-left" href="#">
                        <img class="media-object" src="http://placehold.it/64x64" alt="">
                    </a>
                    <div class="media-body">
                        <h4 class="media-heading">Start Bootstrap
                            <small>August 25, 2014 at 9:30 PM</small>
                        </h4>
                        Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                        <!-- Nested Comment -->
                        <div class="media">
                            <a class="pull-left" href="#">
                                <img class="media-object" src="http://placehold.it/64x64" alt="">
                            </a>
                            <div class="media-body">
                                <h4 class="media-heading">Nested Start Bootstrap
                                    <small>August 25, 2014 at 9:30 PM</small>
                                </h4>
                                Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                            </div>
                        </div>
                        <!-- End Nested Comment -->
                    </div>
                </div>
            </div>

胡安五

根据您的代码和您的评论,我假设该页面名为post.php并且当您第一次到达该页面时,它在 url 上的 id 如下所示:post.php?id=156

但是一旦您提交了评论表单,由于该表单的操作只是post.php,您就会丢失 id。

您可以在发布后在操作上添加 id:

<form role="form" method="post" action="post.php?id=<?php echo $id; ?>">

或添加一个带有 id 的隐藏输入:

<input type="hidden" name="id" value="<?php echo $id; ?>">

但是你必须用 $_POST 到达它

另一种选择是使用 SELF 进行如下操作:

 <form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

这将保留 id 以及您在 url 上可能拥有的任何其他 get 变量。

//编辑

这是正在运行的问题的简化,如果您访问 post.php?id=456(或任何数字),然后按提交,您会得到正确的响应:

<!-- First Blog Post -->
<?php 

    if(isset($_POST["create_comment"])){

        $post_id = $_GET["id"];
        echo "The id is: $post_id";

        // gets comment and inserts into the db

    }

    if(isset($_GET["id"])){
        $id = $_GET["id"];
        // calls display_single_post    
    }

?>

<!-- Comments Form -->
<div class="well">
    <h4>Leave a Comment:</h4>
    <form role="form" method="post" action="post.php?id=<?php echo $id; ?>">
        <button type="submit" name="create_comment" class="btn btn-primary">Submit</button>
    </form>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用setInterval()刷新我的php页面的一部分

如何使用php将数据传递到我的html页面的另一部分?

使用php提取链接的一部分?

更新 .php 页面的一部分

在 PHP 中分解文件名以使用其中的一部分

使用PHP获取URL的最后一部分

使用PHP在Golang中打印网页源代码的一部分

如何使用PHP获取字符串的一部分

如何使用php xmlreader读取XML文件的仅一部分

PHP使用已知文件名的一部分移动文件

是否有可能使用php即时重写rss feed的一部分

使用PHP变量加载视图的一部分

如何使用preg_replace(php)剥离给定URL的一部分

将字符串分为两部分:第一部分是标题,第二部分是使用php的章节

PHP-<form action =“”>属性内的变量作为get方法的url的一部分

php:// output-仅输出页面的一部分而不是整个页面

如何使用 PowerShell Get-Content -replace 仅替换一部分字符串

Java:将字符串转换为在 url 中使用作为 get 的一部分

<a>使用mailgun链接仅发送PHP电子邮件中的URL的一部分

AJAX / PHP重新加载页面的一部分

在php中多次打印html页面的一部分

索引错误-适用于我的代码的一部分,而不适用于另一部分

使用Paw提取登录页面后面的URL的一部分

使用Laravel单击<a>元素仅更改页面的一部分?

安卓。Webview。使用Javascript显示页面的一部分

使用 Django 仅更新 HTML 页面的一部分

如何使用AnyEvent :: HTTP仅获取页面的一部分?

如何使用jQuery刷新页面的一部分

使用ajax重新加载页面的一部分