SQLSTATE[23000]: Integrity Constraint Violation: 1062 Duplicate entry '0' for key 'PRIMARY'

Asked

Viewed 7,609 times

3

When trying to perform an INSERT in my table with PDO, I get the following error: SQLSTATE[23000]: Integrity Constraint Violation: 1062 Duplicate entry '0' for key 'PRIMARY'

I’ve researched enough and seen some similar problems, but all for lack of AUTO_INCREMENT in the table, however. My chart has AUTO_INCREMENT and is with the right PRIMARY-KEY. I even made an INSERT in the hand in the same way that is being done in the code and the INSERT worked.

Follows 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 from 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 successfully rescaled at hand:

MariaDB [memorando]> insert into usuarios (usua_email, usua_password) values ('[email protected]','??????');
Query OK, 1 row affected (0.05 sec)
  • 1

    It is with php that gives error?

2 answers

2

Check if your table primary key column is as "auto increment", if it is not changing, I had the same error here in my Magento, I fixed this way.

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, try to delete the record that has ID 0.

Another problem is you are trying to register a user with a previously registered email, because of the parameter UNIQUE (usua_email)

The best thing to do if this is not the case would be to print the query in the console before the run, to analyze it.

  • Thanks for the answer. But I believe that it is not any of these points. Until I performed the manual Insert, there was no record in the table. Therefore, it is also not the point of the usual field_email to be UNIQUE. I made a print_r on the connection variable before the execution but the query is identical to the one I made in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.