0

when i refresh page auto submit . how to stop insert data when page refresh ? i want insert data when click add button and without redirect another page. plz help me. i want to came page display show data.

<?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>
Duminda
  • 13
  • 2
  • 7
  • 1
    Possible duplicate of [Best way to avoid the submit due to a refresh of the page](http://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-to-a-refresh-of-the-page) – Niklesh Raut Jan 25 '17 at 10:14

2 Answers2

0

You could redirect the user to other/same page after submit logic has been run. After that, refreshing shouldn't submit the form again.

Siim
  • 180
  • 7
0

A little addition to what Siim said: you can do this with header("Location: name_of_file.php"); after $stmt->execute();