Unity和MySQL无法注册许多中文

时间:2019-08-02 04:16:44

标签: php mysql unity3d

我正在注册并登录。

当我输入两个以上的中文单词时,它无法注册。 只能注册一个中文单词。

我在phpMyadmin的用户名中添加了中文单词,它可以登录!

为什么我一开始不能注册很多中文?

请帮助我!

PHP:

 <?php

 $con = mysqli_connect('xxxx:3306', 'root', 'xxxx', 'xxxx');

 if(mysqli_connect_errno())
 {
     echo "1: Connection failed";
     exit();
 }

 $username = $_POST["name"];
 $password = $_POST["password"];
 $gender = $_POST["gender"];


 $namecheckquery = "SELECT username FROM players WHERE username='" . $username . "';";

 $namecheck = mysqli_query($con, $namecheckquery) or die("2: Name check query failed");


 if (mysqli_num_rows($namecheck) > 0)
 {
     echo "3:Name already exists";
     exit();
 }

 $salt = "\$5\$rounds=5000\$" . "steamedhams" . $username . "\$";
 $hash = crypt($password, $salt);
 $insertuserquery = "INSERT INTO players (username, hash,salt,gender) VALUES 
  ('" . $username."', '" . $hash . "', '". $salt."','".$gender."');";
 mysqli_query($con, $insertuserquery) or die ("4: Insert player query failed");

 echo ("0"); ?>

我在Unity中的代码

public class Registration : MonoBehaviour
{
    public InputField nameField;
    public InputField passwordField;

    public Button submitButton;
    public Button boyButton;
    public Button girlButton;

    public void CallRegister()
    {
        StartCoroutine(Register());
    }

    IEnumerator Register()
    {
        WWWForm form = new WWWForm();
        form.AddField("name", nameField.text);
        form.AddField("password", passwordField.text);

        WWW www = new WWW("http://localhost/sqlconnect/register.php", form);
        yield return www;
        if(www.text =="0")
        {
            Debug.Log("User created successfully");
            UnityEngine.SceneManagement.SceneManager.LoadScene("mainmenu");
        }
        else
        {
            Debug.Log("User creation failed. Error #" + www.text);
        }
    }

    public void VerifyInputs()
    {
        submitButton.interactable = (nameField.text.Length >= 1 && passwordField.text.Length >= 4);
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定,但是我的猜测是WWW不会自动转义所提供的form中的空格或出现编码错误。

通常,您应该尝试使用UnityWebRequest.Post来明确指出:

  

postData中的数据将被转义,然后通过System.Text.Encoding.UTF8解释为字节流。

这可能会解决您的问题

private IEnumerator Register()
{
    WWWForm form = new WWWForm();
    form.AddField("name", nameField.text);
    form.AddField("password", passwordField.text);

    using(var www = UnityWebRequest.Post("http://localhost/sqlconnect/register.php", form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
            return;
        }

        if(string.Equals(www.downloadHandler.text, "0"))
        {
            Debug.Log("User created successfully");
            UnityEngine.SceneManagement.SceneManager.LoadScene("mainmenu");
        }
        else
        {
            Debug.Log("User creation failed. Error #" + www.text);
        }
    }
}