Mysql Duplicate Key Error Number
Contents |
Connectors More MySQL.com Downloads Developer Zone Section Menu: Documentation Home MySQL 5.5 Reference Manual Preface and Legal mysql error codes list Notices General Information Installing and Upgrading MySQL Tutorial MySQL
Mysql Sqlstate
Programs MySQL Server Administration Security Backup and Recovery Optimization Language Structure Globalization Data Types
Mysql Errors And Solutions
Functions and Operators SQL Statement Syntax The InnoDB Storage Engine Alternative Storage Engines High Availability and Scalability Replication MySQL Cluster NDB 7.2 Partitioning Stored
Mysql Error Php
Programs and Views INFORMATION_SCHEMA Tables MySQL Performance Schema Connectors and APIs Extending MySQL MySQL Enterprise Edition MySQL Workbench MySQL 5.5 Frequently Asked Questions Errors, Error Codes, and Common Problems Sources of Error Information Types of Error Values Server Error Codes and Messages Client Error Codes and Messages Problems mysql error code 1062 and Common Errors How to Determine What Is Causing a Problem Common Errors When Using MySQL Programs Access denied Can't connect to [local] MySQL server Lost connection to MySQL server Client does not support authentication protocol Password Fails When Entered Interactively Host 'host_name' is blocked Too many connections Out of memory MySQL server has gone away Packet Too Large Communication Errors and Aborted Connections The table is full Can't create/write to file Commands out of sync Ignoring user Table 'tbl_name' doesn't exist Can't initialize character set File Not Found and Similar Errors Table-Corruption Issues Administration-Related Issues Problems with File Permissions How to Reset the Root Password What to Do If MySQL Keeps Crashing How MySQL Handles a Full Disk Where MySQL Stores Temporary Files How to Protect or Change the MySQL Unix Socket File Time Zone Problems Query-Related Issues Case Sen
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business mysql error 1062 duplicate entry for key primary Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation mysql sqlstate 45000 Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like mysql errors log you, helping each other. Join them; it only takes a minute: Sign up Handle error for duplicate entries - PHP MySQL up vote 20 down vote favorite 2 I have a PHP form which enters data into https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html my MySQL database. My primary key is one of the user-entered values. When the user enters a value that already exists in the table, the MySQL error "Duplicate entry 'entered value' for key 1" is returned. Instead of that error, I would like to alert the user that they need to enter a different value. Just an echoed message or something. I guess my question comes down to: how to turn a specific MySQL error into http://stackoverflow.com/questions/3146838/handle-error-for-duplicate-entries-php-mysql a PHP message Thanks edit: nickf's answer below is nice, but is there any way to discern between specific errors? php mysql error-handling share|improve this question edited Jun 30 '10 at 6:39 asked Jun 30 '10 at 6:09 RobHardgood 103116 add a comment| 3 Answers 3 active oldest votes up vote 31 down vote accepted To check for this specific error, you need to find the error code. It is 1062 for duplicate key. Then use the result from errno() to compare with: mysql_query('INSERT INTO ...'); if (mysql_errno() == 1062) { print 'no way!'; } A note on programming style You should always seek to avoid the use of magic numbers (I know, I was the one to introduce it in this answer). Instead, you could assign the known error code (1062) to a constant (e.g. MYSQL_CODE_DUPLICATE_KEY). This will make your code easier to maintain as the condition in the if statement is still readable in a few months when the meaning of 1062 has faded from memory :) share|improve this answer edited Jun 30 '10 at 7:14 answered Jun 30 '10 at 6:47 jensgram 22.8k25675 1 Yeah, that's a good point. This didn't work though... are you sure 1022 is the right error code? I'll start looking too. Thanks –RobHardgood Jun 30 '10 at 7:10 Ah, it's 1062, cf. dev.mysql.com/do
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the http://stackoverflow.com/questions/13132535/mysql-duplicate-entry-error-even-though-there-is-no-duplicate-entry company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up MySQL duplicate entry error even though there is no duplicate entry up vote 5 down vote favorite 1 I am using mysql error MySQL 5.1.56, MyISAM. My table looks like this: CREATE TABLE IF NOT EXISTS `my_table` ( `number` int(11) NOT NULL, `name` varchar(50) NOT NULL, `money` int(11) NOT NULL, PRIMARY KEY (`number`,`name`) ) ENGINE=MyISAM; It contains these two rows: INSERT INTO `my_table` (`number`, `name`, `money`) VALUES (1, 'S. Name', 150), (2, 'Another Name', 284); Now I am trying to insert another row: INSERT INTO `my_table` (`number`, `name`, `money`) VALUES (2, 'S. Name', 240); And MySQL mysql error code just won't insert it while telling me this: #1062 - Duplicate entry '2-S. Name' for key 'PRIMARY' I really don't understand it. The primary key is on the first two columns (both of them), so the row I am trying to insert HAS a unique primary key, doesn't it? I tried to repair the table, I tried to optimize the table, all to no avail. Also please note that I cannot change from MyISAM to InnoDB. Am I missing something or is this a bug of MySQL or MyISAM? Thanks. To summarize and point out where I think is the problem (even though there shouldn't be): Table has primary key on two columns. I am trying to insert a row with a new combination of values in these two columns, but value in column one is already in some row and value in column two is already in another row. But they are not anywhere combined, so I believe this is supposed to work and I am very confused to see that it doesn't. mysql primary-key mysql-error-1062 duplicates share|improve this question edited Oct 30 '12 at 4:32 asked Oct 30 '12 at 4:13 user1763581 1451314 Are those the exact schema and exact INSERTs? If not, we may be barking up the wrong trees! Please provide a r