0

I thought the most efficient way was to create a UNIQUE field on the table instead of selecting to check for existing values before doing anything else but this makes use of two queries. Instead with a UNIQUE field only one query is necessary because MySQL checks for you. The problem is that duplicate entry errors cause an internal server error which I cannot recover from in PHP. What do you guys suggest, what is the best way to avoid duplicate entries in a PHP & MySQL application?

10
  • It is better to check and confirm for duplicacy before you run your insert query Commented May 1, 2014 at 4:47
  • @nedstark Wouldn't it be better with a UNIQUE constraint, faster? Commented May 1, 2014 at 4:48
  • "The problem is that duplicate entry errors cause an internal server error which I cannot recover from in PHP" that sounds odd, please explain Commented May 1, 2014 at 4:49
  • @Dagon exactly how it sounds, I am using CodeIgniter btw. Every time I try to insert the same information already in a field with a UNIQUE constraint, for testing, I get in return an internal server error 500. I have tried try/catch blocks but the program just stops execution... Commented May 1, 2014 at 4:51
  • theres going to be some error message somewhere, find it. Commented May 1, 2014 at 4:52

4 Answers 4

2
  1. Use ON DUPLICATE KEY

INSERT INTO someTable (id, amount) VALUES ($to_uid, $send_amount) ON DUPLICATE KEY UPDATE amount = amount + $send_amount

https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

2) You can catch the duplicate key exception. PDO example:

try{
    $stmt->execute(...);
}
catch(PDOException $e){
    if($e->errorInfo[1] == 1062){
        // Mysql returned 1062 error code which means a duplicate key
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but what about mysqli or CodeIgniter which is what I am using? A plain try/catch does not work.
Because mysqli doesn't throw exceptions by default. Mysqli functions just return false. See this answer if you want make mysqli throw exceptions on error stackoverflow.com/questions/14578243/… Or just check mysqli::$errno for 1062 code when for example mysqli::query returns false
Guess I will be asking how to do this in CodeIgniter.
0

You could use REPLACE INTO for your query, it will try an insert first and than it will delete the row with the same ID and replace it.

1 Comment

That is not what I want. I do not want to replace anything I just want to avoid putting in information that is already there. You said duplicate errors could not be recovered from, I think Dagon has a different idea.
0

FOUND THE SOLUTION!

CodeIgniter requires the setting

$db['default']['stricton'] = TRUE;

an explicitly calling

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

In order for MySQL to throw exceptions. The exceptions must also be caught.

2 Comments

I mentioned about mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); in my answer comments. Cool guy did his answer. Great
@Didar_Uranov I thank you for your help but this was actually more a of a CodeIgniter issue which you failed to resolve. Without the setting I mentioned MySQL would not have been able to throw exceptions.
0

You can use INSERT IGNORE to prevent updating a row and prevent an exception from being thrown if row already exists.

https://dev.mysql.com/doc/refman/5.5/en/insert.html

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error occurs. Ignored errors may generate warnings instead, although duplicate-key errors do not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.