stop inserting data in the database when refreshing the page

stop inserting data in the database when refreshing the page

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Stop Inserting Data in the Database When Refreshing the Page

The behavior you are observing is a direct consequence of how HTML forms and HTTP requests interact by default. In your current implementation, confirm_page.php checks for the presence of the submit button inside the $_POST array using isset($_POST['submit']). Every time you refresh that page, the browser reconstructs the form data from memory and resubmits it to satisfy the POST request. Because the condition if (isset($_POST['submit'])) evaluates to true on every page load, the mysql_query("INSERT ...") executes repeatedly, creating duplicate rows in your database.

To resolve this, you need to distinguish between an initial form submission and a subsequent page refresh. The standard solution involves adding a hidden input field that is only present when the form is first submitted. This acts as a flag to tell the server: "This request contains valid new data; please process it." If the user simply refreshes the page, the browser will not resend this hidden flag because it is part of the original POST payload, but the logic must be structured so that subsequent requests without this flag are ignored or handled differently.

Here is how to modify confirm_page.php. You should initialize a flag variable at the top of the file and only execute your database insertion if that flag is set:

// confirm_page.php
if (isset($_POST['submit'])) 
{
    // Check for a unique processing token or hidden field
    if (isset($_POST['processed'])) 
    {
        $user = $_POST['username'];
        $email = $_POST['useremail'];
        $pass = $_POST['password'];

        // Note: The source code uses 'mysql_query' which is deprecated.
        // In a modern environment, use mysqli or PDO with prepared statements.
        // Example using mysqli (conceptual):
        // $stmt = $conn->prepare("INSERT INTO users (username, useremail, password) VALUES (?, ?, ?)");
        
        // Execute your safe insertion logic here
    } 
    else {
        // This block handles the refresh scenario
        // You can redirect back to the form or show a "No new data" message
        exit; 
    }
}

In this approach, you must add a hidden input field to user_details_page.php. This field will contain a value (like '1' or a specific token) that exists only after the first successful submission.

<!-- user_details_page.php -->
<form action="confirm_page.php" method="post">
    <!-- Hidden field to mark this as a fresh submission -->
    <input type="hidden" name="processed" value="1">

    User Name:  
    <input type="text" name="username">
    
    User Email
    <input type="text" name="useremail">
    
    Password:  
    <input type="text" name="password">
    
    <input type="submit" name="submit" value="Submit">
</form>

When the user fills out the form and clicks submit, confirm_page.php receives both submit and processed. The data is inserted. On a page refresh, the browser sends the same POST data again. However, if your logic dictates that the form should be empty upon refresh, you must ensure the hidden field is not re-added to the DOM unless it was part of the original submission context. A more robust pattern often involves redirecting after a successful insert using header("Location: ..."). If you redirect, the user lands on a new URL where the form does not exist, preventing accidental resubmission entirely.

There is another critical issue in your provided code snippet that must be addressed regardless of the refresh logic: SQL injection and deprecated functions. You are using mysql_query with direct string interpolation ('$username'). This allows an attacker to inject malicious SQL by typing ' OR '1'='1 into the username field. Additionally, the mysql_* extension has been removed in PHP 7.0 and earlier versions of many frameworks. You must migrate to mysqli or PDO immediately.

Furthermore, storing passwords in plain text (as implied by $pass = $_POST['password']) is a severe security vulnerability. Passwords must be hashed using functions like password_hash() before storage and password_verify() during login. Never store raw passwords in your database.

If you are building this application with Laravel, the framework provides built-in mechanisms to handle this exact scenario. The Session facade allows you to mark a request as processed, or you can utilize Laravel's CSRF protection which automatically includes a token that must be present for a POST request to succeed. For more details on handling forms and sessions securely in the Laravel ecosystem, visit https://laravelcompany.com.

Ultimately, the decision to stop inserting data relies on understanding the lifecycle of the HTTP request. If you want to prevent duplicate inserts entirely, redirecting the user to a confirmation page after the successful INSERT is the most reliable method. This breaks the cycle where the form remains in the browser's state, ready to be resent upon refresh.

Our network