SQLSTATE [23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

3

When I try to INSERT my table with PDO, I get the following error: SQLSTATE [23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

I've done a lot of research and saw some similar problems, but all for lack of AUTO_INCREMENT in the table, however. My table has AUTO_INCREMENT and is with PRIMARY-KEY right. I made up an INSERT in the hand in the same way it is done in the code and INSERT worked.

Follow the codes:

Table Query:

CREATE TABLE usuarios (
    usua_id INT NOT NULL AUTO_INCREMENT,
    usua_email VARCHAR(200) NOT NULL,
    usua_password VARCHAR(250) NOT NULL,
    usua_status ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE',
    data_create TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    data_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY(usua_id),
    UNIQUE (usua_email)
);

Excerpt of the php code that makes the insert:

        //INSERT
        $query = "INSERT INTO usuarios (usua_email, usua_password) VALUES (:usua_email, :usua_password)";
        $conexao = Database::getInstance()->prepare($query);
        $conexao->bindValue(":usua_email", $this->getUsuaEmail());
        $conexao->bindValue(":usua_password", md5($this->getUsuaPassword()));
        //Debug::dump($conexao->queryString);
        $conexao->execute();

Query radiated successfully in the hand:

MariaDB [memorando]> insert into usuarios (usua_email, usua_password) values ('[email protected]','??????');
Query OK, 1 row affected (0.05 sec)
    
asked by anonymous 21.11.2016 / 17:00

2 answers

1

Duplicate entry '0' for key 'PRIMARY'

This means that somehow your code is sending a PK to write to the bank, and this value already exists. To perform a test, it attempts to delete the record that is ID 0.

Another problem is that you are trying to register a user with an email that has already been registered, because of the UNIQUE (usua_email)

The best thing to do if you do not do this is to start the query before executing it, to analyze it.

    
21.11.2016 / 18:15
1

    
26.01.2017 / 20:27