为什么我会收到 MySQL 错误 1062?
在我的 php 页面之一上,我不断收到以下错误:
SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”重复条目“0”
我不知道为什么或在哪里发生这种情况,这就是我发布此问题的原因。
我认为它发生在这个代码序列的某个地方。
// create user
$STH = $DBH -> prepare( "insert into users ( display_name, oauth_provider, oauth_uid ) values ( :value, :oauth_provider, :id )" );
$STH -> bindParam( ':value', $value, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':id', $oauth_id, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':oauth_provider', $oauth_provider, PDO::PARAM_STR, 255 );
$STH -> execute();
// get newly created user
$STH = $DBH -> prepare( "select * from users where oauth_uid = :id and ( display_name = :value or email = :value ) and oauth_provider = :oauth_provider" );
$STH -> bindParam( ':value', $value, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':id', $oauth_id, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':oauth_provider', $oauth_provider, PDO::PARAM_STR, 255 );
$STH -> execute();
$result = $STH -> fetch();
// create settings record with 0's
$STH = $DBH -> prepare( "insert into settings ( col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, user_id ) values ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, :id )" );
$STH -> bindParam( ':id', $result["id"], PDO::PARAM_INT, 4 );
$STH -> execute();
如何找出导致问题的部分?
On one of my php pages, I keep getting the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'
I don't know why or where this is happening, which is the reason I am posting this question.
I think it's happening in this code sequence somewhere.
// create user
$STH = $DBH -> prepare( "insert into users ( display_name, oauth_provider, oauth_uid ) values ( :value, :oauth_provider, :id )" );
$STH -> bindParam( ':value', $value, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':id', $oauth_id, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':oauth_provider', $oauth_provider, PDO::PARAM_STR, 255 );
$STH -> execute();
// get newly created user
$STH = $DBH -> prepare( "select * from users where oauth_uid = :id and ( display_name = :value or email = :value ) and oauth_provider = :oauth_provider" );
$STH -> bindParam( ':value', $value, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':id', $oauth_id, PDO::PARAM_STR, 255 );
$STH -> bindParam( ':oauth_provider', $oauth_provider, PDO::PARAM_STR, 255 );
$STH -> execute();
$result = $STH -> fetch();
// create settings record with 0's
$STH = $DBH -> prepare( "insert into settings ( col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, user_id ) values ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, :id )" );
$STH -> bindParam( ':id', $result["id"], PDO::PARAM_INT, 4 );
$STH -> execute();
How do I find out which part is causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它来自mysql,与代码无关
,再次删除主键列将auto_increment更改为1并
再次添加键
it is from mysql no relation to the code
drop the column of primary key alter auto_increment to 1 again and
add the key again