Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Remove Space after inserting data to MySql

Discussion in 'Scripting' started by mfarisammar, Apr 8, 2021.

  1. mfarisammar

    mfarisammar

    Joined:
    Mar 30, 2020
    Posts:
    6
    Hello everyone, i'm not sure if this is the correct place to ask, do inform me if its not.

    So im starting to use MySQL with my unity multiplayer game as my database.

    Im currently making a register and login system for my players to save their progress. However, when player successfully register , i noticed that the data that was send to the database contain an empty space (" ") just before the data value. This make the login system failed due to not detecting the empty space when login in.

    This is my code ( for security reason, i wouldnt put some of variable value )

    I really appreciate if anyone can help me in this matter, its driving me crazy for 1 week already.
    Thank you

    *Tried trimming / replace string / Not including special character

    Register C#

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.Networking;
    7. using TMPro;
    8.  
    9. public class Registeration : MonoBehaviour
    10. {
    11.     public TMP_InputField emailField;
    12.     public TMP_InputField NicknameField;
    13.     public TMP_InputField UsernameField;
    14.     public TMP_InputField PasswordField;
    15.     public TMP_InputField CPasswordField;
    16.     private static int RMvalue = 1000;
    17.  
    18.     public TMP_Text DebugText;
    19.  
    20.     public Button RegisterButton;
    21.  
    22.     void Start()
    23.     {
    24.         DebugText.text = "";
    25.     }
    26.     public void CallRegister()
    27.     {
    28.         if( emailField.text == null)
    29.         {
    30.             DebugText.text = "Email must not be empty";
    31.         }
    32.         else if(NicknameField.text == null)
    33.         {
    34.             DebugText.text = "NickName must not be empty";
    35.         }
    36.         else if(UsernameField.text == null)
    37.         {
    38.             DebugText.text = "Username must not be empty";
    39.         }
    40.         else if(PasswordField.text == null)
    41.         {
    42.             DebugText.text = "Password must not be empty";
    43.         }
    44.         else if(CPasswordField.text == null)
    45.         {
    46.             DebugText.text = "Confirm Password must not be empty";
    47.         }
    48.         else if(PasswordField.text != CPasswordField.text)
    49.         {
    50.             DebugText.text = "Password must be the same as confirm Password";
    51.         }
    52.         else
    53.         {
    54.             StartCoroutine("Register");
    55.         }
    56.        
    57.     }
    58.  
    59.     IEnumerator Register()
    60.     {
    61.         WWWForm form = new WWWForm();
    62.         form.AddField("username",UsernameField.text);
    63.         form.AddField("email",emailField.text);
    64.         form.AddField("nickname",NicknameField.text);
    65.         form.AddField("password",PasswordField.text);
    66.         form.AddField("RM",RMvalue);
    67.         //WWW www = new WWW("***");
    68.         using (UnityWebRequest www = UnityWebRequest.Post("***",form))
    69.         {
    70.             yield return www.SendWebRequest();
    71.  
    72.             if(www.error == null)
    73.             {
    74.                 string result = www.downloadHandler.text;
    75.                 Debug.Log(result);
    76.                 if(result.Contains("0") == true)
    77.                 {
    78.                     DebugText.text = "Success";
    79.                 }
    80.                 else
    81.                 {
    82.                     DebugText.text = "Failed";
    83.                     Debug.Log(www.downloadHandler.text);
    84.                 }
    85.             }
    86.         }
    87.        
    88.  
    89.         if(www.text == "0")
    90.         {
    91.             DebugText.text = "User created successfully.";
    92.             //SceneManager.LoadScene(0);
    93.         }
    94.         else
    95.         {
    96.             DebugText.text = "User creation failed. Error #" + www.text ;
    97.         }
    98.        
    99.     }
    100.  
    101.     public void VerifyInputs()
    102.     {
    103.         RegisterButton.interactable = (UsernameField.text.Length >= 8 && CPasswordField.text.Length >= 7);
    104.     }
    105. }
    106.  
    Register PHP

    Code (PHP):
    1. <?php
    2.  
    3.     $con = mysqli_connect('***', '***', '***', '***');
    4.  
    5.     //Checking connection
    6.     if(mysqli_connect_errno())
    7.     {
    8.         echo "1: Connection failed";
    9.         exit();
    10.     }
    11.  
    12.     //$username = trim( ($_POST["username"]));
    13.     //$username = str_replace(' ', '', $username);
    14.     $username = $_POST["username"];
    15.     $email =  $_POST["email"];
    16.     $nickname =   $_POST["nickname"];
    17.     $password =   $_POST["password"];
    18.     $RM =   $_POST["RM"];
    19.  
    20.     //Checking previous query
    21.  
    22.     $namecheckquery = "SELECT `username` FROM `player` WHERE `username` = '" .$username."'";
    23.  
    24.     $namecheck = mysqli_query($con, $namecheckquery) or die ("2: Name check query failed");
    25.  
    26.     if(mysqli_num_rows($namecheck) > 0)
    27.     {
    28.         echo "3: Name already exists";
    29.         exit();
    30.     }
    31.  
    32.     $salt = "\$5\$rounds=5000\$" . "steamedhams" . $username . "\$";
    33.     $hash = crypt($password, $salt);
    34.    
    35.     //$username = str_replace(' ', '', $username);
    36.     $insertuserquery = "INSERT INTO `player` (`username`,`nickname`,`email`,`hash`,`salt`,`RM`) VALUES (' " .$username. " ' , ' " .$nickname. " ' , ' " .$email. " ' ,' " .$hash. " ' , ' " .$salt. " ',' " .$RM. " ')";
    37.     mysqli_query($con, $insertuserquery) or die ("4 : Insert player query failed");
    38.  
    39.     echo("0");
    40.  
    41. ?>
    Picture reference
    https://imgur.com/a/m3E6fzY
     
  2. mfarisammar

    mfarisammar

    Joined:
    Mar 30, 2020
    Posts:
    6
    Just a heads up, i do found an alternative way but it is improper to do it

    -> add space manually in the code itself, so still waiting for the solution.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It looks to me that you're adding a space to your username value in your select query on line 22 in the 2nd script. You've got a space within the quotes which I don't think should be there. But it has been a while since I've done much SQL or PHP.
     
  4. mfarisammar

    mfarisammar

    Joined:
    Mar 30, 2020
    Posts:
    6
    Yeah that was my first thought. However, i'll already triple check the code for both C# and PHP and even when i input the data in unity editor myself. i do observe that every data that was inserted to the table have a black space which was weird. But i still appreciate your help though.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Ah, I didn't realize you meant the extra space was actually inserted in the DB when registering. I think I misread your first post.

    I would output $insertuserquery after line 36 of the php, then check if there actually is an extra space in the insert query. Browsers often hide extra spaces when they render webpages (when there are 2 or more spaces you see just 1 space), so if viewing from a browser make sure you view source instead of just the browser output.