doukong1897 2013-11-04 03:49
浏览 21
已采纳

添加到数据库。 刷新时不重复

I have this code:

Episode.php

    <?$feedback = new feedback;
$articles = $feedback->fetch_all();

      if (isset($_POST['name'], $_POST['post'])) {
             $cast = $_GET['id'];
             $name = $_POST['name'];
             $email = $_POST['email'];
             $post = nl2br ($_POST['post']);
             $ipaddress = $_SERVER['REMOTE_ADDR'];

if (empty($name) or empty($post)) {
             $error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO comments (cast, name, email, post, ipaddress) VALUES(?, ?, ?, ?, ?)');
     $query->bindValue(1, $cast);
     $query->bindValue(2, $name);
     $query->bindValue(3, $email);
     $query->bindValue(4, $post);
     $query->bindValue(5, $ipaddress);

     $query->execute();
} }?>
<div align="center">
<strong>Give us your feedback?</strong><br /><br />

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="episode.php?id=<?php echo $data['cast_id']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" /> / <input type="text" name="email" placeholder="Email" /><small style="color:#aa0000;">*</small><br /><br />
<textarea rows="10" cols="50" name="post" placeholder="Comment"></textarea><br /><br />
<input type="submit" onclick="myFunction()" value="Add Comment" />
<br /><br />
<small style="color:#aa0000;">* <b>Email will not be displayed publicly</b></small><br />
</form>

</div>

Include.php

class feedback { public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM comments");
      $query->bindValue(1, $cast);
      $query->execute(); return $query->fetchAll();
              } }

This code updates to the database as it is suppose to. But after submission it reloads the current page as mentioned in the form action.

But when I refresh the page to see the comment being added it asks to re submit. If I hit submit then the comment adds again.

How can I stop this from happening?

Maybe I could hide the comment box and display a thank you message but that would not stop a repeat entry.

Please help. Thank you.

Kev

  • 写回答

2条回答 默认 最新

  • duanjiao6730 2013-11-04 04:07
    关注

    You need to add a redirect in there. So at the bottom of your POST block add

    if(isset($_POST['name'], $_POST['post'])) {
        // Do POST stuff here
    
        header('Location: your/url/here');
        exit;
    }
    

    This sends a 302 redirect to the browser and it does a clean load of the page. Since this is a GET operation, there's no reload issues either.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据