PHP - Check if email and username already exists in MySQL Database -
i tried lot making code work registration.php page, want check whether email , username registered on database. tried checking username , worked fine want check whether email registered or not. regarding highly appreciated. below code registration.php, hoping help! thanks!
<?php /* our "config.inc.php" file connects database every time include or require within php script. since want script add new user our db, talking our database, , therefore, let's require connection happen: */ require("config.inc.php"); //if posted data not empty if (!empty($_post)) { //if username or password empty when user submits //the form, page die. //using die isn't practice, may want //displaying error message within form instead. //we front-end form validation within our android app, //but have have back-end code double check. if (empty($_post['username']) || empty($_post['password']) || empty($_post['fullname']) || empty($_post['emailadd'])) { // create data json response $response["success"] = 0; $response["message"] = "please fill in fields!"; //die kill page , not execute code below, //display parameter... in case json data our android //app parse die(json_encode($response)); } //if page hasn't died, check our database see if there //already user username specificed in form. ":user" //a blank variable change before execute query. //do way increase security, , defend against sql injections $query = " select 1 users username = :user , eadd = :email"; //now lets update :user should $query_params = array( ':user' => $_post['username'], ':email' => $_post['eadd'] ); //now let's make run query: try { // these 2 statements run query against database table. $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch (pdoexception $ex) { // testing, use die , message. //die("failed run query: " . $ex->getmessage()); //or use use 1 product json data: $response["success"] = 0; $response["message"] = "database error1. please try again!"; die(json_encode($response)); } //fetch array of returned data. if data returned, //we know username in use, murder our //page $row = $stmt->fetch(); if ($row) { // testing, use die , message. //die("the username in use! try different username!"); //you comment out above die , use one: $response["success"] = 0; $response["message"] = "the username in use! try different username!"; die(json_encode($response)); } //if have made here without dying, in clear //create new user. let's setup our new query create user. //again, protect against sql injects, user tokens such :user , :pass $query = "insert users ( username, password, fullname, eadd ) values ( :user, :pass, :fulnme, :email )"; //again, need update our tokens actual data: $query_params = array( ':user' => $_post['username'], ':pass' => $_post['password'], ':fulnme' => $_post['fullname'], ':email' => $_post['eadd'] ); //time run our query, , create user try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch (pdoexception $ex) { // testing, use die , message. //die("failed run query: " . $ex->getmessage()); //or use use one: $response["success"] = 0; $response["message"] = "database error2. please try again!"; die(json_encode($response)); } //if have made far without dying, have added //a new user our database. few things here, such //redirect login page. instead going echo out //json data read android application, login //the user (or redirect different activity, i'm not sure yet..) $response["success"] = 1; $response["message"] = "registration successful!"; echo json_encode($response); //for php webservice simple redirect , die. //header("location: login.php"); //die("redirecting login.php"); } else { ?> <h1>register</h1> <form action="register.php" method="post"> full name<br /> <input type="text" name="name" value="" /> <br /><br /> email address<br /> <input type="text" name="eadd" value="" /> <br /><br /> username:<br /> <input type="text" name="username" value="" /> <br /><br /> password:<br /> <input type="password" name="password" value="" /> <br /><br /> <input type="submit" value="register" /> </form> <?php } ?>
in code show check if there in database having same username , same email,
example:
you have in database
toto, toto@gmail.com toto2, toto2@gmail.com
with test registering
toto,toto2@gmail.com
your test won't find because there nobody has toto , toto2@gmail.com email address,
you need make 2 separate stmt, 1 user name , 1 email address
Comments
Post a Comment