如何在 php pdo 中刷新页面后停止插入

  • 本文关键字:插入 刷新 php pdo php
  • 更新时间 :
  • 英文 :


当我刷新页面时自动提交. 页面刷新时如何停止插入数据? 我想在单击添加按钮时插入数据,而不重定向另一个页面。请帮助我。我想来页面显示显示数据。

<?php
        try{
        $host='localhost';
        $name='pdo';
        $user='root';
        $pass='';
        $dbc = new PDO("mysql:host=$host;dbname=$name",$user,$pass);
        }
        catch(PDOException $e)
        {
        echo $e->getMessage();
        }
        //---------------------------------------------------------------------
        if (isset($_POST['add']))
        {
        $stmt = $dbc->prepare("insert into user (frist,last,web) values (:frist,:last,:web)");
        $stmt->bindParam(':frist',$_POST['frist']);
        $stmt->bindParam(':last',$_POST['last']);
        $stmt->bindParam(':web',$_POST['web']);
        $stmt->execute();
         }
        ?>
        <!DOCTYPE html>
        <html>
        <head>
        <title> the digital craft - PDO</title>
        </head>
        <body>
        <header>
        <h1>Mysql PDO</h1>
        </header>
        <div>
        <form method="post">
         <input name="frist" placeholder="frist name">
         <input name="last" placeholder="last name">
         <input name="web" placeholder="web site">
         <button name="add" type="submit">Add</button>
        </form>
        </div>
        <hr>
        <table>
         <thead>
           <tr>
           <th>ID</th>
           <th>Frist Name</th>
           <th>Last Name</th>
           <th>Website</th>
           <th>Save</th>
           </tr>
         </thead>
         <tbody>
         <?php 
         $stmt=$dbc->query("SELECT * FROM user");
         $stmt->setFetchMode(PDO::FETCH_ASSOC);
         while($data=$stmt->fetch()){
        ?>
         enter code here<tr>
           <td><input name="id" value="<?=$data['id']?>"></td>
           <td><input name="frist" value="<?=$data['frist']?>"></td>
           <td><input name="last" value="<?=$data['last']?>"></td>
           <td><input name="web" value="<?=$data['web']?>"></td>
           <td><button name="save" type="submit">save</button></td>
          </tr>
          <?php } ?>
         </tbody>
        </table>
        </body>
        </html>

您可以在运行提交逻辑后将用户重定向到其他/同一页面。之后,刷新不应再次提交表单。

对Siim所说的话稍作补充:您可以在$stmt->execute();后使用 header("Location: name_of_file.php"); 执行此操作

最新更新