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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题