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

时间:2014-12-07 04:10:02

标签: 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

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

我的评论.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.how我可以将用户重定向到同一帖子但没有任何错误......

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

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

1 个答案:

答案 0 :(得分:1)

首先尝试这个评论部分获取图片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);
}

注意根据需要管理您的代码,我只是给您重定向的解决方案。上面的代码对我来说很好......