php - 将用户重定向到 php 中的同一页面问题

标签 php mysql pdo blogs

我正在用 php、mysql(PDO) 创建一个博客。在博客中,我创建了这样的逻辑,即当新用户到来时,它将首先登录,之后他/她将能够看到帖子和评论。现在我在评论部分面临一个问题...... 我创建了一个评论表,每个用户评论都将保存在其中,当管理员批准评论时,评论将显示在关注页面上。

主要问题:

当用户评论页面重新加载并且我收到一些通知时,我知道通知的原因,原因是当用户单击该帖子以详细查看其内容时,该帖子将代表 Id 打开, 但是当用户对帖子发表评论并重新加载页面时,页面找不到相关的帖子ID,并且会显示通知...

这是我的评论表图片 enter image description here

这是我的帖子页面代码....

<?php
session_start();
include 'conn.php';
if(!isset($_SESSION['user']))
{
    header('location:signin.php');
}
$smt=$conn->prepare("SELECT * FROM post,images WHERE post.Id=images.Id");
$smt->execute();
?>
<?php include 'header.php';?>
<?php
if(isset($_SESSION['user']))
{
    include 'nav.php';
}
else
{
    include 'nav-simple.php';
}
?>

    <div class="container-fluid">
    <?php include 'right_sidebar.php';?>
<div class="main-container-top" id="masonry-grid">
    <?php while($rows=$smt->fetch(PDO::FETCH_OBJ)):
        ?>

        <div class="col-md-3 grid-item post-col">
<img src="image/<?php echo $rows->Image_Name;?>" data-echo="image/<?php echo $rows->Image_Name;?>" class="post-image"/>
            <h5>Post On &nbsp;<?php echo $rows->Post_Date;?></h5>
            <a href="post-description.php?id=<?php echo $rows->Id ?>" <h4><?php echo $rows->Post_Title;?></h4></a>
            <p>
                <?php echo $rows->Post;?>
            </p>

        </div>

    <?php endwhile;

    ?>
</div>
    </div>

<?php include 'footer-content.php';?>
<?php include 'footer.php';?>

下面是我的帖子描述页面代码,其中有一个评论部分,这是我遇到问题的页面...

<?php
session_start();
include 'conn.php';
$pic_id='';
if(isset($_GET['id']))
{
    $pic_id=$_GET['id'];
}

$smt=$conn->prepare("SELECT * FROM post,images WHERE post.Id = images.Id AND post.Id='".$pic_id."'");
$smt->execute();
$rows=$smt->fetch(PDO::FETCH_OBJ);
$title=$rows->Post_Title;
$img=$rows->Image_Name;
$post=$rows->Post;;
?>
<?php include 'header.php';?>

<?php include 'nav.php';?>

<div class="container-fluid">
    <?php include 'right_sidebar.php';?>
<div class="col-md-1"></div>

    <div class="col-md-9 main-container-top container">

        <div class="media col-md-12 description-post">


           <img src="image/<?php echo $img;?>" alt="<?php echo $img;?>" class="img-rounded img-responsive media-left img-description"/>
            <div class="media-body">
               <h4 class="h4 description-heading"><?php echo $title;?></h4>
                <p class="post-text text-justify text-info">

                    <?php echo $post;?>
            </div>

        </div>

        <br/>

            <div class="media col-md-12 comment-section">
<?php
if(isset($_SESSION['comment-error']))
{
?>
    <span class="alert alert-warning col-md-6 container col-md-offset-3"><?php echo $_SESSION['comment-error']; ?></span>
    <?php
}
unset($_SESSION['comment-error']);
 if(isset($_SESSION['comment-success']))
{
?>
    <span class="alert alert-success col-md-6 container col-md-offset-3"><?php echo $_SESSION['comment-success']; ?></span>
    <?php
}
unset($_SESSION['comment-success']);
    ?>
                <br/>
                <span class="col-md-1 comment-pic"><img src="profile%20picture/cv.jpg" alt="post image" class="img-thumbnail img-responsive comment-img"/></span>
                <div class="media-body  comment-head col-md-10">
                    <h6 class="h6">comment by <a href="#">e</a> on Thursday 9.00 PM</h6>
                    <p class="comment"></p>
                </div>
            </div>

        <br/>
        <div class="col-md-12 container">
            <h4 class="description-heading h4 text-muted">Share your thought</h4>
            <br/>
            <form action="comment.php" method="post" class="col-md-12" id="commentForm">
                <textarea name="comment" id="" cols="100" rows="5" placeholder="Your comment"></textarea>

                <br/>
                <input type="submit" value="Post" name="cmsg" id="<?php echo $pic_id;?>" class="btn btn-info"/>
                <br/>
            </form>
        </div>
        <br/>



</div>

</div>
<?php include 'footer-content.php';?>
 <?php include 'footer.php';?>

在帖子描述页面中有一个评论部分,用户在这里发表评论,但是当评论发表时它会显示一些通知.. 例子..

发表评论前

enter image description here

评论后... enter image description here

您可以清楚地看到,当评论发布并且页面重新加载时,它会显示通知..

我的comment.php代码

<?php
session_start();
include 'conn.php';
$comment=$_POST['comment'];
$status='Pending for approvial';
$smt=$conn->prepare("INSERT INTO user_comment(Comment,Status,On_Time)VALUES ('".$comment."','".$status."',now())");
$smt->execute();
if($smt)
{
    $_SESSION['comment-success']='Your Comment is Pending for approvial';
    header('location:post-description.php');
}
else
{
    $_SESSION['comment-error']='There si something wrong with your account';
    header('location:post-description.php');
}
?>

我想要什么:

我可以在用户评论后显示成功消息,但我无法解决通知问题,因为当页面重新加载时,它将找不到帖子 ID。我如何将用户重定向到同一个帖子但没有收到任何错误...

希望这个细节足以理解问题...

任何博客、评论、用户相关的想法都将受到赞赏...

最佳答案

首先尝试在评论部分获取图像 ID,.....

 <form action="comment.php" method="post" class="col-md-12" id="commentForm">
                <textarea name="comment" id="" cols="100" rows="5" placeholder="Your comment"></textarea>

                <input type="hidden" name="image-id" value="<?php echo '?id='.$pic_id;?>"/>
                <br/>
                <input type="submit" value="Post" name="cmsg" id="" class="btn btn-info"/>
                <br/>
            </form>

其次,将此添加到您的 comment.php 代码中....

$pic=$_POST['pic-id'];
if($smt)
{
    $_SESSION['comment-success']='Your Comment is Pending for approvial';
    header('location:post-description.php'.$image_id);
}
else
{
    $_SESSION['comment-error']='There si something wrong with your account';
    header('location:post-description.php'.$image_id);
}

注意 随意管理你的代码,我只是给你重定向的解决方案。上面的代码对我来说很好用...

关于php - 将用户重定向到 php 中的同一页面问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339510/

相关文章:

php - 交响乐 2 : Setting a cookie for the current session upon login

mysql - 存储大量数据的最佳方式

mysql - ODS导入MySQL : offset data

mysql删除第n个位置的字符

php - 如何提高MySql数据库处理数据的速度

sql - PDO从表中选择WHERE url!=(选择)

mysql - 使用 mySQL 和 php PDO 更新数据库

php - 如何从 Nette 中的 NEON 文件中获取值?

php - 提交搜索表单后未收到来自数据库的查询结果

PHP:计算重复项并删除重复行