mysql - Custom php script PDO is throwing exeption 23000,1062 duplicate entry -
i working on php script using pdo insert info in mysql. getting error "23000",1062,"duplicate entry 'email@email.com-username' key 'email' inserting info in database.
so here php codes
if(isset($_post['email'])){ $this->db = new connect(); $this->db = $this->db->dbconnect(); $this->encryption = new encryption(); isset($_post['timezone']) , $_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 `users` ( id, email, uname, password, regtime, sessionkey, country, region, browser, ip ) values ( (select max(id) + 1 `users` `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 getting error echoing pdo exception error $regmessage = array('error'=>$e); } header('content-type: application/json'); echo json_encode($regmessage); }else{ header('location: /'); }
at error, showing me duplicate entry of emailid + username key email looks email@email.com-username
but in info base, getting email id in email column , username in username column. can 1 tell me whats wrong in codes?
my users table construction
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 ;
so can tell me , wrong?
thank helping me.
the phrasing in error message: 'email@email.com-username' key 'email'
straight corresponds unique key unique key 'email' ('email','uname')
. line, creating compound key, can think of invisible column in index comprised of email-uname. there not column added table format, , seeing expected behavior email , uname treated separately in table , key.
if want test on , on 1 time again same email , username combo, you'll need delete row every time. without doing this, error seeing expect see if post-ing same info on , on again.
i want mention have (appropriately) specified id column auto_increment, calculating value manually. discourage doing this, , instead utilize null insert value. mysql utilize right key value in column, , avoid potential key collision if ever had 2 of these things executing @ same exact moment.
php mysql pdo
No comments:
Post a Comment