Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate key Entry when no values are duplicates

Tags:

sql

php

mysql

pdo

I am getting a very weird duplicate entry error when no values are even identical...

INSERT INTO offer_status (id,user_id,sql_id,disabled) 
VALUES ('8854011812','8854','11812','0')

Returns:

#1062 - Duplicate entry '2147483647' for key 'PRIMARY'

This does not make sense to me at all!

Note: This code is not normally executed, I am just trying to debug. Normally in my PHP I have:

$offer=$campid;
$id=$user_id.'0'.$offer;
$sql="INSERT INTO offer_status (id,user_id,sql_id,disabled) VALUES (?,?,?,?)
ON DUPLICATE KEY UPDATE disabled=VALUES(disabled)";

    $db->prepare($sql)->execute(array($id, $user_id, $offer, 0));

id is the primary key. What is going on here?

Added: Result of DESC offer_status:

Field   Type    Null    Key Default Extra
id  int(12) NO  PRI NULL    
user_id int(12) NO      NULL    
sql_id  int(12) NO  MUL NULL    
favor   tinyint(4)  NO      0   
disabled    tinyint(4)  NO      0   
pref    int(2)  NO      0   
like image 305
kmoney12 Avatar asked Dec 09 '22 18:12

kmoney12


2 Answers

8854011812 is out of int range. It will be replaced by the max int number which is 2147483647.

The second time you insert a value greather than the int max, you will get a duplicate key error.

If you need such high numbers you can change your data type to bigint.

like image 98
juergen d Avatar answered Dec 11 '22 07:12

juergen d


Oh i see. Since you have mentioned that ID is the primary, I guess the data type of which is int and which the maximum value for SIGNED INT is 2147483647. You should have change the data type to decimal(15,0) or VARCHAR(15) so the value will be cattered.

The value you are inserting 8854011812 is kinda out of range to which an int can hold.

like image 45
John Woo Avatar answered Dec 11 '22 07:12

John Woo