PHP PDO 密钥的重复条目 'PRIMARY'
PHP PDO Duplicate entry for key 'PRIMARY'
我正在尝试一次将值插入不同的 table。之前从未向这些 table 中的任何一个插入过新数据,我被插入到第一个 table 中,然后才得到这个错误
Duplicate entry 'example@mail.com' for key 'PRIMARY'
代码
include("../connect.php");
$this->con->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
//insert into another database
include("../another_directory/connect.php");
$this->con->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
print_r($this->con->errorInfo());
null 引用第一个字段 user_id
即 index
并设置为 auto increment
。电子邮件字段是 primary key
。现在我正在尝试注册用户,但用户只能插入到第一个 table 中,而不能插入到另一个数据库中的相同 table 中,并得到重复的主要错误,尽管它从未被添加过之前。
我尝试了传统的插入方式
insert into users (field1, field2) values ($value1, $value2)
但是我得到了同样的错误。
如何解决这个问题?
可能对连接使用不同的值:
// Connection data (server_address, database, name, poassword)
$hostdb = 'localhost';
$namedb = 'database1';
$userdb = 'user';
$passdb = 'pass';
// Connection data2 (server_address, database, name, poassword)
$hostdb1 = 'localhost';
$namedb1 = 'database2';
$userdb1 = 'user';
$passdb1 = 'pass';
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn1 = new PDO("mysql:host=$hostdb1; dbname=$namedb1", $userdb1, $passdb1);
$conn->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
$conn1->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
print_r($conn->errorInfo());
print_r($conn1->errorInfo());
我正在尝试一次将值插入不同的 table。之前从未向这些 table 中的任何一个插入过新数据,我被插入到第一个 table 中,然后才得到这个错误
Duplicate entry 'example@mail.com' for key 'PRIMARY'
代码
include("../connect.php");
$this->con->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
//insert into another database
include("../another_directory/connect.php");
$this->con->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
print_r($this->con->errorInfo());
null 引用第一个字段 user_id
即 index
并设置为 auto increment
。电子邮件字段是 primary key
。现在我正在尝试注册用户,但用户只能插入到第一个 table 中,而不能插入到另一个数据库中的相同 table 中,并得到重复的主要错误,尽管它从未被添加过之前。
我尝试了传统的插入方式
insert into users (field1, field2) values ($value1, $value2)
但是我得到了同样的错误。
如何解决这个问题?
可能对连接使用不同的值:
// Connection data (server_address, database, name, poassword)
$hostdb = 'localhost';
$namedb = 'database1';
$userdb = 'user';
$passdb = 'pass';
// Connection data2 (server_address, database, name, poassword)
$hostdb1 = 'localhost';
$namedb1 = 'database2';
$userdb1 = 'user';
$passdb1 = 'pass';
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn1 = new PDO("mysql:host=$hostdb1; dbname=$namedb1", $userdb1, $passdb1);
$conn->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
$conn1->exec('INSERT INTO users VALUES (null, "' . $this->email . '","' . $this->name . '","' . $pass . '",0,' . $this->isEmailConfirmed . ',"' . $this->token . '", "")');
print_r($conn->errorInfo());
print_r($conn1->errorInfo());