mysql - PHP exception handling after SQL insert -


i creating simple registration form in php, when user tries register there popup javascript alert succes or fail message.

now want catch sql exception show if username or email excists in database instead of standard fail message.

this code have far:

if(isset($_post['btn-signup'])) {   $uname = mysql_real_escape_string($_post['uname']);   $email = mysql_real_escape_string($_post['email']);   $upass = md5(mysql_real_escape_string($_post['pass']));    if(mysql_query("insert user(username,password,email) values('$uname','$upass','$email')")) { ?>     <script>alert('successfully registered ');</script> <?php } else{ ?>     <script>alert('error while registering you...');</script> <?php  } } ?> 

how can check if email or username excists in database? both variable's unique in database.

from comments:

i don't want 2 queries while database can return exception me. if there 10 million records in table, don't want check them before inserting new one.

ok, have 1 query insert , check unique? have insert on unique_index mysql column, can catch these sort of exceptions following style of answer shameless stolen answer this question:

in case of answer we'll assume you're using pdo, because should. please read it.

// pre-setup database connection somewhere, include (or class) $link = new pdo("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword); // pdo needs set in exception mode: $link->setattribute( pdo::attr_errmode, pdo::errmode_exception );  //input processing functions. // (entirely optional) $uname = mycleanerfunction($_post['uname']); $email = mycleanerfunction($_post['email']); //please see note below re:md5 //$upass = md5($_post['pass']);  $options['cost'] = 12; $upass = password_hash($_post['pass'],password_bcrypt,$options);  //now reach code part:     try {         //pdo query execution goes here:           $statement = $link->prepare("insert user(username,password,email) values(:uname, :email, :pass)"));          $statement->bindvalue(":uname", $uname);          $statement->bindvalue(":email", $email);          $statement->bindvalue(":pass", $upass);          $statement->execute();          //reaching here ok!      }     catch (\pdoexception $e) {         if ($e->errorinfo[1] == 1062) {             // insert query failed due key constraint violation.             // means failed because primary key              // (the email) appears in database table.         }         if($e->errorinfo[1] == 9999){           // possible other if clauses other errors in insert.         }     } 

you read catching , outputting pdo errors. mysql unique key constraints.

  • also useful alternative viewpoint should not catch pdo exceptions.

  • also please note md5 extremely weak hash storing passwords , php password_hash function much preferred way of doing it.

  • please use prepared statements mysql interactions, layout above rough guide how , similar mysqli , pdo. prepared statements go long way towards securing data malicious user input.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -