自定义PHP脚本PDO抛出异常23000,1062重复条目

时间:2014-10-19 17:23:47

标签: php mysql pdo

我正在开发一个PHP脚本,我正在使用PDO在mySQL中插入数据。我收到错误“23000”,1062,“重复输入'email@email.com-username'用于键'email'但是它将数据插入数据库。

所以这是我的PHP代码:

if(isset($_POST['email'])){
            $this->db = new connect();
            $this->db = $this->db->dbConnect();
            $this->encryption = new Encryption();

            isset($_POST['timezone']) AND $_POST['timezone'] != 'null' ? date_default_timezone_set($_POST['timezone']): date_default_timezone_set('America/Chicago');

            $this->email        = $_POST['email'];

            $this->username     = $_POST['username'];

            $this->password     = $this->encryption->encode($_POST['password']);

            $this->dTime        = date("Y-m-d H:i:s");;

            $this->sessionKey   = $_POST['key'];

            $this->country      = $_POST['country'];

            $this->region       = $_POST['uregion'];

            $this->browser      = $_POST['browser'];

            $this->ip           = $_POST['accessFrom'];

            $regMessage = array('error'=>false);

            try{

            $query = "INSERT INTO `users` (

                        id, email, uname, password, regtime, sessionkey, country, region, browser, ip

                        ) VALUES (

                        (SELECT MAX(id) + 1 FROM `users` AS `maxId`), :email, :uname, :password, :regtime, :sessionkey, :country, :region, :browser, :ip

                        )";

            $register = $this->db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));


            if($this->sessionKey === $_SESSION['token']){

                $register->bindParam(':uname', $this->username);     
                $register->bindParam(':email', $this->email);   
                $register->bindParam(':password', $this->password); 
                $register->bindParam(':regtime', $this->dTime); 
                $register->bindParam(':sessionkey', $this->sessionKey); 
                $register->bindParam(':country', $this->country);
                $register->bindParam(':region', $this->region); 
                $register->bindParam(':browser', $this->browser); 
                $register->bindParam(':ip', $this->ip);
                $register->execute();       

                    if($register->rowCount() > 0){

                        $regMessage = array('error'=>false);

                    }else{
                        $regMessage = array('error'=>true);
                    }                   

                }else{

                    throw new PDOException ('Error');

                    }

            }

            catch(PDOException $e){



//this is where I am getting error so I am echoing pdo exception error 

 $regMessage = array('error'=>$e);



            }

            header('Content-Type: application/json');

            echo json_encode($regMessage);

        }else{

            header('Location: /');
        }

出现错误,它显示重复输入emailid +用户名的密钥电子邮件,看起来像email@email.com-username

但是在数据库中,我只在电子邮件列中收到电子邮件ID,只在用户名栏中收到用户名。 那么任何人都可以告诉我我的代码中有什么问题吗?

我的用户表格结构是

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(250) CHARACTER SET utf8 NOT NULL,
  `uname` varchar(20) CHARACTER SET utf8 NOT NULL,
  `password` varchar(100) CHARACTER SET utf8 NOT NULL,
  `regtime` datetime NOT NULL,
  `sessionkey` varchar(10) CHARACTER SET utf8 NOT NULL,
  `country` varchar(25) CHARACTER SET utf8 NOT NULL,
  `region` varchar(25) CHARACTER SET utf8 NOT NULL,
  `browser` varchar(25) CHARACTER SET utf8 NOT NULL,
  `ip` varchar(16) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`,`uname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

所以有人能告诉我哪里和哪里出错了?

感谢您帮助我。

2 个答案:

答案 0 :(得分:0)

错误消息中的措词:'email@email.com-username' for key 'email'直接对应于您的唯一键UNIQUE KEY 'email' ('email','uname')。使用该行,您将创建一个复合键,您可以将其视为索引中由email-uname组成的不可见列。您的表格中不会添加使用此格式的列,并且您会看到电子邮件和取消名称在表格中单独处理并一起处理密钥的预期行为。

如果您想使用相同的电子邮件和用户名组合一遍又一遍地进行测试,则每次都需要删除该行。如果不这样做,您所看到的错误就是我一遍又一遍地发布相同数据所期望的错误。

我还想提一下,你已经(适当地)指定了你的id列是AUTO_INCREMENT,但是你手动计算了这个值。我想阻止你这样做,而是使用NULL作为插入值。 MySQL将在此列中使用正确的键值,如果您在同一时刻执行了其中两项操作,则可以避免发生键冲突的可能性。

答案 1 :(得分:0)

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `uname` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `password` varchar(100) CHARACTER SET utf8 NOT NULL,
  `regtime` datetime NOT NULL,
  `sessionkey` varchar(10) CHARACTER SET utf8 NOT NULL,
  `country` varchar(25) CHARACTER SET utf8 NOT NULL,
  `region` varchar(25) CHARACTER SET utf8 NOT NULL,
  `browser` varchar(25) CHARACTER SET utf8 NOT NULL,
  `ip` varchar(16) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

这是解决方案。