Development
How to Prevent Duplicate Form Submissions From Creating Duplicate Rows

Prevent duplicate form submissions at the database boundary using a unique request identity. Disabling the submit button and redirecting after POST improve the interface, but neither prevents concurrent requests or retries after a network timeout.
The database must decide whether this is a new operation or a repeat of the same one. Give the operation a stable key and enforce uniqueness within the correct user and action scope.
Create one identity for one intended submission
Generate a random submission key when rendering the form and retain it while the visitor retries that same operation. After success, render a new key for the next intentional operation.
Bind the key to the authenticated user and action. Validate its shape and length, and keep CSRF protection separate. A CSRF token can be valid for many operations; an idempotency key represents one intended mutation.
For a simple InnoDB design, store the key on the business row:
CREATE TABLE requests (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
request_key CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
payload_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
message TEXT NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY one_request (user_id, request_key)
) ENGINE=InnoDB;
The binary collation makes key comparison exact. The unique constraint is the concurrency guarantee; an earlier SELECT followed by an unguarded INSERT is still a race.
Match the retry to its original payload
Normalize validated fields into a stable representation and hash that representation. Store the hash with the request key. If the same key arrives with different business fields, reject it as a conflict rather than silently returning the old result.
For example, a form edit after a failed attempt may represent a new intended operation. The UI needs to obtain a new key or explicitly abandon the old attempt; the server should not guess.
Use only fields that define the business operation in this hash. A rotating CSRF token, transient timestamp, or display-only field should not turn an otherwise identical retry into a different payload.
Insert first and handle the specific conflict
Attempt the insert using a prepared statement. If it succeeds, return the new record's identifier. If the database reports a duplicate on the expected unique constraint, load the existing row within the authenticated user's scope, compare the payload hash, and return its identifier when the payload matches.
Do not catch every integrity error and call it a duplicate submission. A missing foreign key or another unique constraint can represent a different defect. Inspect the driver-specific error information and confirm that the expected row exists before treating the result as a retry.
MySQL's CREATE TABLE documentation describes unique indexes; PHP's PDO error information explains how driver error details are exposed.
If the operation creates multiple rows, put the idempotency record and those changes in one transaction. Another request must never observe an operation as complete before its required records commit.
Keep external effects separate
A unique local row does not automatically stop duplicate remote charges or emails. Save an outbox job in the same transaction, then let a worker use a stable business key when communicating with a remote system.
If the remote API supports idempotency, follow its documented key scope and retention period. If it does not, define reconciliation for an unknown outcome after a timeout. Exactly-once claims require more than a local request table.
After successful POST, redirect to a result page. Keep the original key long enough to cover realistic retries, and document retention. Deleting deduplication records immediately after success removes their protective value.
Test concurrent and uncertain outcomes
Send the same key concurrently and confirm that one row exists and both successful responses reference it. Repeat with a different payload and require a conflict. Test two users with the same key, a timeout after commit, and a retry after process restart.
For surrounding boundaries, see incremental PHP refactoring. Discuss a data-consistency review when duplicate records are causing operational cleanup.