2

I am working on a project of my own but I have an issue.

I created a simple table with the query:

$sql_art = "CREATE TABLE articoli (
    id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    cod_int VARCHAR(6) NOT NULL,
    nome VARCHAR(50) NOT NULL,
    descr VARCHAR(100) NULL,
    cod_barre INT(20) NOT NULL,
    prezzo INT(5) NULL,
    note VARCHAR(100) NULL,
    reg_date TIMESTAMP,
    UNIQUE (cod_barre)
)";

when I try to add a new record with

$sql_ins = "INSERT INTO `gestionale_db`.`articoli` 
(`id`, `cod_int`, `nome`, `descr`, `cod_barre`, `prezzo`, `note`, `reg_date`) 
VALUES 
(NULL, 'A001', 'Cazzilli', 'ancora caldi', '4545415615456',
   '215', 'su tisi', CURRENT_TIMESTAMP)";

it return the error

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

but obviously cod_barre is not equal to this value. If I try to modify the record with UPDATE, it modify all the value except cod_barre that remain the same. By default in php.ini I use InnoDB es ENGINE and latin1_swedish_ci as char_set. Can anyone help me to find the error?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RickyLeRoi
  • 117
  • 9
  • 1
    Possible duplicate of [Incorrect Integer (2147483647) is inserted into MySQL?](http://stackoverflow.com/questions/10255724/incorrect-integer-2147483647-is-inserted-into-mysql) – CodeGodie Mar 11 '16 at 15:39
  • 1
    @CodeGodie : for mysql INT(x) = INT always (I mean 4 bytes anyway) – boctulus Mar 11 '16 at 15:40
  • @CodeGodie it was a different or I can't understand it, alexander.polomodov resolve the issue – RickyLeRoi Mar 11 '16 at 15:47

1 Answers1

1

In your case you go to INT maximum 2147483647 = 2^{31}-1, because 4545415615456 is greater then 2147483647.

So you really insert row with max int value. You need to create this table to fix your error:

$sql_art = "CREATE TABLE articoli (
    id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    cod_int VARCHAR(6) NOT NULL,
    nome VARCHAR(50) NOT NULL,
    descr VARCHAR(100) NULL,
    cod_barre BIGINT(20) NOT NULL,
    prezzo INT(5) NULL,
    note VARCHAR(100) NULL,
    reg_date TIMESTAMP,
    UNIQUE (cod_barre)
)";
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
  • I've got the same error with numbers like 0000048921 where all the initials 0 were deleted or numbers like 65412865 that is less then 2147483647. However, it works. thanks a lot! – RickyLeRoi Mar 11 '16 at 15:39