MeSoVarchar
08-08-2006, 02:28 AM
Hi every1. I have just build a user system but cant figure out how to check if say a unique username/password exists before accepting data from the form. Any ideas?
|
Click to See Complete Forum and Search --> : Checking if record exists before insert MeSoVarchar 08-08-2006, 02:28 AM Hi every1. I have just build a user system but cant figure out how to check if say a unique username/password exists before accepting data from the form. Any ideas? MeSoVarchar 08-08-2006, 03:03 AM okey, here is my snippet: <? db con $UserName = $_POST['UserName']; $Password = $_POST['Password']; /* DB CONNECTION CODE HERE */ $sql = mysql_query("SELECT * FROM Clients WHERE username='$username'"); $result = mysql_num_rows($sql); if($result !=="0"){ echo "The username you have chosen already exists!"; exit; } else { $query = "INSERT INTO Clients (id, FirstName, LastName, Email, Tel, Address, City, PostalCode, Country, UserName, Password ) VALUES ('$id', '$FirstName', '$LastName', '$Email', '$Tel', '$Address', '$City', '$PostalCode', '$Country', '$UserName', '$Password')"; mysql_query($query) or die('mysql error.'); echo "Thank you"; echo " "; } ?> The damn code still wont work! Says ends at exit even when i have a unique username! bokeh 08-08-2006, 05:06 AM Password should not need to be unique, only the username. Make the username column type UNIQUE. Then do the INSERT (no prior tests) and then check for a duplicate key error.$result = mysql_query($insert_query); if(($result !== true) and (mysql_errno() == 1062) { # username already in use } elseif($result !== true) { # query failed for another reason } else { # INSERT worked ok } aussie girl 08-08-2006, 10:40 AM try it without the double quotes around the zero if($result !== 0){ echo "The username you have chosen already exists!"; exit; webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |