donglu5000 2015-01-19 02:09
浏览 93
已采纳

刷新页面时,表单数据重新发布到数据库

I'm trying to learn how to make a registration form that publishes data to a database. I couldn't get the code from a tutorial to work until I stripped it down. It now works OK, except that if I refresh the page (to remove the values I typed in so I can type in another entry), it republishes the original data to the database table.

In other words, if I type in the values Kelly Kau for firstname and lastname, then hit the Submit button, Kelly and Kau are entered in the database table. But when I refresh the page, another row with Kelly and Kau is added.

Is there a way to stop this? I'm working with PHP and MySQL. The tutorial I'm trying to copy is jQuery. Eventually, I may add some AJAX, too.

<form id="signupform" autocomplete="off" method="post" action="" novalidate>
  <table>
    <tr>
    <td class="label"><label id="lfirstname" for="firstname">First Name</label></td>
    <td class="field"><input id="firstname" name="firstname" type="text" value="" maxlength="100"></td>
    <td class="status"></td>
  </tr>
  <tr>
    <td class="label"><label id="llastname" for="lastname">Last Name</label></td>
    <td class="field"><input id="lastname" name="lastname" type="text" value="" maxlength="100"></td>
    <td class="status"></td>
  </tr>
  <tr>
    <td class="label"><label id="lsignupsubmit" for="signupsubmit">Signup</label></td>
    <td class="field" colspan="2"><input id="signupsubmit" name="signup" type="submit" value="Signup"></td>
  </tr>
</table>
</form>
<?php
include('ajax-database/config.php');
$pdo = connect();

try {
  $sql = "INSERT INTO g1_members (firstname, lastname) VALUES  (:firstname, :lastname)";
 $query = $pdo->prepare($sql);
 $query->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
 $query->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);

 $query->execute();
} catch (PDOException $e) {
 echo 'PDOException : '.  $e->getMessage();
}
?>
  • 写回答

2条回答 默认 最新

  • dongliling6336 2015-01-19 02:48
    关注

    Use a conditional statement based on your submit button being clicked:

    <?php
    include('ajax-database/config.php');
    $pdo = connect();
    
    if(isset($_POST['signup'])){
    
    try {
      $sql = "INSERT INTO g1_members (firstname, lastname) VALUES  (:firstname, :lastname)";
     $query = $pdo->prepare($sql);
     $query->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
     $query->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);
    
     $query->execute();
    
    } catch (PDOException $e) {
     echo 'PDOException : '.  $e->getMessage();
    }
    
    } // brace for if(isset($_POST['signup']))
    
    ?>
    

    or a header redirection on successful query:

    if($query){
    header("Location: http://www.example.com");
    exit;
    }
    

    • Ideally, if you don't want the same values to be inserted, then it would be best to set the given column(s) as UNIQUE.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取