Search Unity

Problems with user registration code

Discussion in 'Multiplayer' started by Davdoc, Apr 21, 2017.

  1. Davdoc

    Davdoc

    Joined:
    Apr 6, 2015
    Posts:
    14
    I am following a tutorial about programming a simple login/ register menu. So far it can: Check database for user and log them in if the credentials match, register a user who is not in the database yet, verify passwords to ensure they match, and check if the password is too short.

    So the problem is, the only part that is not working is when I try to enter a user who is already in the database. This error is thrown:

    ArgumentException: JSON parse error: Invalid value.
    UnityEngine.JsonUtility.FromJson[User] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:24)
    AuthenticationManager+<RequestRegistration>c__Iterator1.MoveNext () (at Assets/AuthenticationManager.cs:127)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)


    Now the good part is that the user does not get added, and I know that the database side is fine (the primary keys and unique keys do not allow multiple of the same email), so there is something wrong with either my unity script or my php script. By the way, if you look at the end of my unity script, you can see a commented section. This is the code which was being used before the User class was added, and the registration worked fine then...Here are the scripts (I did not include the entire Unity script since I'm sure there is nothing wrong outside of what I posted, but if requested, I will provide it):

    Code (CSharp):
    1.  
    2.  
    3. [Serializable]
    4. public class User{
    5.     public bool success;
    6.     public string error;
    7.     public string email;
    8. }
    9.  
    10.     public IEnumerator RequestRegistration(){
    11.         string email = textEmail.text;
    12.         string password = textPassword.text;
    13.         string reenterPassword = textReenterPassword.text;
    14.      
    15.         if(password.Length < 8){
    16.             textFeedback.text = "Password must be at least 8 characters long.";
    17.             yield break;
    18.         }
    19.         if(password != reenterPassword){
    20.             textFeedback.text = "Passwords do not match.";
    21.             yield break;
    22.         }
    23.      
    24.         form = new WWWForm();
    25.         form.AddField("email",email);
    26.         form.AddField("password",password);
    27.      
    28.         WWW w = new WWW("http://localhost/pdo_action_register.php", form);
    29.         yield return w;
    30.          
    31.         if(string.IsNullOrEmpty(w.error)){
    32.             User user = JsonUtility.FromJson<User>(w.text);
    33.             if(user.success == true){
    34.                 if(user.error != ""){
    35.                     textFeedback.text = user.error;
    36.                 }else{
    37.                     textFeedback.text = "Registration successful.";
    38.                 }
    39.             }else{
    40.             textFeedback.text = "An error occured.";
    41.             }
    42.         }
    43.      
    44.         /*
    45.         if(string.IsNullOrEmpty(w.error)){
    46.             if(w.text.ToLower().Contains("user already exists")){
    47.                 textFeedback.text = "The email address is already in use.";
    48.             }else if(w.text.ToLower().Contains("failed to connect to server")){
    49.                 textFeedback.text = "Failed to connect to server.";
    50.             }else if(w.text.ToLower().Contains("could not create account")){
    51.                 textFeedback.text = "Could not create account at this time. Try again later.";
    52.             }else{
    53.                 textFeedback.text = "Registration Successful.";
    54.             }
    55.         }else{
    56.             textFeedback.text = "An error occured.";
    57.         } */
    58.     }
    59. }
    60.  
    61.  
    Code (CSharp):
    1. <?php
    2.  
    3.     include "pdo_dbconnect.php";
    4.  
    5.     $email = $_POST['email'];
    6.     $upass = $_POST['password'];
    7.  
    8.     //$email = strip_tags($email);
    9.     //$upass = strip_tags($upass);
    10.  
    11.     //$email = 'user5@test.com';
    12.     //$upass = 'password';
    13.  
    14.     $stmt = $db->prepare("SELECT userEmail FROM users WHERE userEmail='$email'");
    15.     $stmt->execute();
    16.     $row = $stmt->fetch(PDO::FETCH_ASSOC);
    17.  
    18.     if($row){
    19.         $dataArray = array('success' => false, 'error' => 'user already exists');
    20.         echo "User already exists.";
    21.     }else{
    22.         $stmt = $db->prepare("INSERT INTO users(userEmail, userPass) VALUES('$email','$upass')");
    23.         $result = $stmt->execute();
    24.         if($result){
    25.             $dataArray = array('success' => true, 'error' => "", 'email' => "$email");
    26.             //echo "User created.";
    27.         }else{
    28.             $dataArray = array('success' => false, 'error' => 'could not create account');
    29.         }
    30.         // Test echo $dataArray['error'];
    31.     }
    32.  
    33.     header('Content-Type:application/json');
    34.     echo json_encode($dataArray);
    35.  
    36.     //test $userEmail = $row['userEmail'];
    37.     //test echo $userEmail;
    38.  
    39. ?>