php - Check if username exists PDO -
How can I examine several compounds instead of checking for each one? So basically I am using the PDO and I have to make sure the username and email are unique. So how will I do this? I have seen
if ($ sthandler-> rowCount ()> gt; {// do something here} but there is one There is a better way to do this. Besides this, if someone can not explain how I work with it
edit This is my query code which inputs in database & lt ;? Php try {$ handler = new PDO ('mysql: host = local host; dbname = s', 'root', '*'); $ Handler- & gt; Set Attribute (PDO: ATTRRAMOD, PDO :: ERRMODE_EXCEPTION); } Hold (PDOException $ e) {exit ($ e-> GetMessage ()); } $ Name = $ _POST ['name']; $ Username = $ _POST ['Username']; $ Email = $ _POST ['email']; $ Password = $ _POST ['password']; $ Password1 = $ _POST ['passwordconf']; $ Ip = $ _SERVER ['REMOTE_ADDR']; // Verifcation (empty (name name) || empty ($ user name) || empty ($ email) || empty ($ password) || empty ($ password1)) "echo all areas"; } // if password match ($ password! = $ Password1) {echo $ passmatch = "password does not match"; } // Email Verification if (! Email at the filter_ ($ email, FILTER_VALIDATE_EMAIL)) {echo $ emailvalid = "Enter valid email"; } // Length of password if (strangel ($ password) & lt; = 6) {echo $ passlength = "6 characters long choose password"; } Function userExists ($ db, $ user) {$ userQuery = "Select * User from info WHERE u.user =: user;"; $ Stmt = $ db- & gt; Ready ($ username); $ Stmt- & gt; Execute (array (': user' = & gt; $ user)); Return !! $ Stmt-> Get (PDO :: FETCH_ASSOC); } $ User = 'userName'; $ Exists = userexist ($ db, $ user); If (exists) {// user already exists} Else {// user does not already exist, you can easily insert it} if (empty ($ passmatch) & amp; below ($ emailvalid) & Amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; less ($ passlength) & lsaquo; Securely insert database in $ cql * User INFO (name, username, email, password, IP) value (: name, username, ,: Password, IP) '; $ Query = $ Handler- & gt; Ready ($ sql); $ Query-> Execute ('Array' (': name' = & gt; $ name, ': username' = & gt; $ username, ': email' = & gt; $ email, ': password' = & gt; $ password, : IP '= & gt; $ ip)); }? & Gt;
& lt ;? Try Php // Connection {$ handler = new PDF ('MySql: host = localhost; dbname = s', 'root', '*'); $ Handler- & gt; Set Attribute (PDO: ATTRRAMOD, PDO :: ERRMODE_EXCEPTION); } Hold (PDOException $ e) {exit ($ e-> GetMessage ()); } $ Name = $ _POST ['name']; $ Username = $ _POST ['Username']; $ Email = $ _POST ['email']; $ Password = $ _POST ['password']; $ Password1 = $ _POST ['passwordconf']; $ Ip = $ _SERVER ['REMOTE_ADDR']; // Verifcation (empty ($ name) || empty ($ user name) || empty ($ email) || empty ($ password) || empty ($ password1)) $ $ error = "complete all fields" ; } // password match if ($ password! = $ Password1) {$ error = "passwords do not match"; } // Email Verification if (! Email (FILTER_VALIDATE_EMAIL); {$ error = "Enter valid email"; } // Length of password if (strangel ($ password) & lt; = 6) {$ error = "Choose a password for 6 characters so far"; } If (! Isset ($ error)) {// no error $ sthandler = $ handler- & gt; Ready (Username by username "where username =: name"); $ Standler- & gt; Baidam Parm (': name', $ user name); $ Sthandler- & gt; Executed (); If ($ sthandler-> punctiquity ()> gt; 0) {resonant "exists! Can not be inserted"; } Else {// securly enter the database $ sql = 'userinfo (name, username, email, password, ip) in values (include: name, username,: email, password,: ip)'; $ Query = $ Handler- & gt; Ready ($ sql); $ Query-> Execute ('Array' (': name' = & gt; $ name, ': username' = & gt; $ username, ': email' = & gt; $ email, ': password' = & gt; $ password, : IP '= & gt; $ ip)); }} And {echo "error occurred:". $ Error; Go out(); }
Comments
Post a Comment