Skip to content
PHP Logo

What is the potential issue with inserting data into a database on page reload in PHP?

The potential issue with inserting data into a database on page reload in PHP is that it can lead to duplicate entries if the user refreshes the page. To solve this issue, you can use a POST-Redirect-GET pattern where after inserting the data into the database, you redirect the user to a different page to prevent the form submission from being resubmitted on page reload.

php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data and insert into database

    // Redirect to a different page to prevent form resubmission on page reload
    header("Location: success.php");
    exit();
}