SOLVED: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

All your question about php
Post Reply
mister_v
Posts: 196
Joined: Thu Mar 04, 2010 9:19 pm

SOLVED: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Post by mister_v »

I have a problem with as script.
From time to time I get

Code: Select all

PHP Fatal error:  Uncaught mysqli_sql_exception: Duplicate entry
A duplicated key is attempted to be created.
I can't really control the input,
Is there a way to keep going even with a duplicated key ?
Last edited by mister_v on Tue Mar 07, 2023 10:06 pm, edited 1 time in total.
chris
Site Admin
Posts: 203
Joined: Mon Jul 21, 2008 9:52 am

Re: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Post by chris »

You first should check why you have a duplicated key.

You can use INSERT IGNORE command rather than the INSERT command.
If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

This should prevent you script form breaking.

Same exists for UPDATE -> UPDATE IGNORE,
But be very careful with this one.
You should verify if the update passed or not,
And act accordingly.
mister_v
Posts: 196
Joined: Thu Mar 04, 2010 9:19 pm

Re: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Post by mister_v »

Thank this helps me a lot.
Post Reply