- Home /
How Do i check for Duplicate username in database
Hi I am trying to implement a simple login system. I am using the WWW method and WWW.Form.
//Create a form
var form = new WWWForm();
form.AddField("uname",userNameEntered);
form.AddField("pass",passwordEntered);
//Put all form data in a variable
var rawData = form.data;
var url = "http://url/login.php";
var w = WWW(url,rawData);
yield w;
if(!w.error)
{
print("There was an error logging in: " + w.error);
}
The problem is when i type in a duplicate username, mysql throws up an error as it should
Error :Duplicate entry 'UnityUser' for key 'PRIMARY'
But the w.error() does not resolve as true ( I am guessing since there is no HTML or PHP error but rahter a MySQL error)
y question is how do i stop the users from using duplicate usernames ?
Answer by jahroy · Nov 24, 2011 at 10:46 PM
You can make your php code return a certain value if there is a mysql error.
You can do this by using the php function named echo.
For example, you could return/echo 1 if a mysql error occurs.
(You might return/echo zero to represent success)
You can then check the return code in Unity by inspecting w.text, which will represent the content of the requested web page.
When you call echo in your php script, you're writing content to a web page.
Answer by Rabbit-Stew-dio · May 11, 2012 at 10:52 AM
Each HTTP request returns a status code. If everything goes well, your HTTP server returns the code "200 - OK".
Your PHP script returns 200 by default, unless you explicitly tell it otherwise. Make sure you set the correct response header. For server side errors, a code in the 5xx range is common, for user errors, a 4xx code is used.
I would return 400 (User error) to indicate that the parameters passed to the server were invalid.
Read the PHP manual on setting server-side response codes.
The status codes can be found in the HTTP-standard, or more conveniently here.
Answer by dineshrajpurohit · Jun 20, 2012 at 03:24 AM
MySql will throw an error only when you try to store value in the database. If the username name is unique in database, sql will throw an error. In your code it seems like you are trying to log in and not tryin to register. In case of login you should not get a duplicate entry error. There has to be something wrong on your PHP code. Check if you are doing an INSERT query or SELECT query on the PHP side.
Answer by shaystibelman · Nov 12, 2012 at 05:25 PM
This is my PHP file for double user verification:
$insertedUsername=$_REQUEST['Username'];
$insertedEmail=$_REQUEST['Email'];
$sqlCheckUsername="SELECT 1 FROM Users WHERE Username = '" . mysql_real_escape_string($insertedUsername) . "'";
$sqlCheckEmail="SELECT 1 FROM Users WHERE Email = '" . mysql_real_escape_string($insertedEmail) . "'";
$queryUsername=mysql_query($sqlCheckUsername,$con) or die('error');
$queryEmail=mysql_query($sqlCheckEmail,$con) or die('error');
$Username = mysql_fetch_assoc($queryUsername);
if(mysql_num_rows($queryUsername)!=0){
echo "Username already exists, choose a different one";
}elseif(mysql_num_rows($queryEmail)!=0){
echo "E-Mail already registered";
}else{
echo "true";
}
It basically checks to see if there already is a row with that username or email in it. I'm using OR, or rather, both controls, so people will know what they should change. Just like when your username OR password are wrong, that way you don't have any doubts for where you're wrong.
Your answer
Follow this Question
Related Questions
Why do I get a newline (\n br) while sending a simple string from php to Unity c# 1 Answer
Login authentication on Unity 5 with php using xampp, and mysqli 0 Answers
C # Does not recognize characters after space 2 Answers
Problem using WWW with PHP and MYSQL 3 Answers
Retrieving data from a WWW php call? 1 Answer