PHP error guide
SQLSTATE 23000 duplicate entry: meaning and fix
Error summary
Treat the unique index as authoritative and handle duplicate races explicitly. An INSERT or UPDATE conflicts with a unique index.
What it means
Treat the unique index as authoritative and handle duplicate races explicitly. An INSERT or UPDATE conflicts with a unique index.
What the error means
This message means that an INSERT or UPDATE conflicts with a unique index. The exact signature distinguishes sqlstate 23000 duplicate entry from a generic application failure.
Why PHP produces it
The engine or service reports “SQLSTATE 23000 duplicate entry” because its required precondition was not met. Treat the unique index as authoritative and handle duplicate races explicitly.
PHP version notes
The sqlstate 23000 duplicate entry wording here is based on PDO with MySQL/MariaDB; exact server wording varies; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: an INSERT or UPDATE conflicts with a unique index
- The code path assumes the prerequisite for sqlstate 23000 duplicate entry has already been satisfied.
- For sqlstate 23000 duplicate entry, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting sqlstate 23000 duplicate entry.
Minimal examples
BAD — reproduces the problem
$pdo->exec("INSERT INTO users(email) VALUES ('ada@example.test')");
FIXED — safer pattern
$stmt = $pdo->prepare("INSERT INTO users(email) VALUES (?)");
$stmt->execute([$email]);
Step-by-step diagnosis
- Copy the complete “SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'ada@example.test' for key 'users.email'” text and retain the first application stack frame.
- Reproduce sqlstate 23000 duplicate entry in the same SAPI and environment listed for this page.
- Before changing sqlstate 23000 duplicate entry, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—treat the unique index as authoritative and handle duplicate races explicitly—then repeat the original request once.
Fixes
Correct the failing prerequisite
Treat the unique index as authoritative and handle duplicate races explicitly
$stmt = $pdo->prepare("INSERT INTO users(email) VALUES (?)");
$stmt->execute([$email]);
Fail explicitly at the boundary
Validate the condition before the operation that emits sqlstate 23000 duplicate entry, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing sqlstate 23000 duplicate entry instead of correcting its upstream condition.
- Testing sqlstate 23000 duplicate entry only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for sqlstate 23000 duplicate entry before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for sqlstate 23000 duplicate entry.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting sqlstate 23000 duplicate entry reproducible.
- Validate external data and service return values before they can trigger sqlstate 23000 duplicate entry.
Web server / environment notes
fpm, cli, docker, linux. The failure occurs where an INSERT or UPDATE conflicts with a unique index
Tags: fpm,cli,docker,linux