Page 1 of 1

SOLVED: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Posted: Mon Mar 06, 2023 7:47 pm
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 ?

Re: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Posted: Mon Mar 06, 2023 8:47 pm
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.

Re: PHP Fatal error: Uncaught mysqli_sql_exception: Duplicate entry

Posted: Tue Mar 07, 2023 10:06 pm
by mister_v
Thank this helps me a lot.