-1

I want to send data from my game to the Database on the server. In this case, I develop locally but when I click the spacebar button The Database is created but With empty data. No data is sent from my game.

I have to try to improve my code also try to implement the pre statements. I am not able to use the unitywebrequest because my version of UNITY3D does not have it my version is 5.0.0f4 When I try to insert the data by hand on fields in the editor and then start the preview, and click the spacebar to send the data, but when I click the database create a row but with empty data, what I fail here, can't find the problem Also, i have aply the debug.log to see if there is any info send from my app to the script and the data is there but i continue not understand why is my code not work correctly, i also provide the prints of the app and database. I also edit again to make the password hash changes i just did but i dont know if is correct like this

!https://imgur.com/J5R56Xf

!https://imgur.com/pzd7t4e

!https://imgur.com/x9VRK6P

This is my unity code below.

using UnityEngine;
using System.Collections;

public class DataInserter : MonoBehaviour {

    public string inputUserName;
    public string inputPassword;
    public string inputEmail;
    public string regdata;

    string CreateUserURL = "localhost/InsertUser.php";

    // Use this for initialization
    void Start () {
        //regdata = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.Space)) CreateUser(inputUserName, inputPassword, inputEmail, regdata);
    }

    public void CreateUser(string username, string password, string email, string regdata){
        WWWForm form = new WWWForm();
        form.AddField("usernamePost", username);
        form.AddField("passwordPost", password);
        form.AddField("emailPost", email);
        form.AddField("regdataPost", regdata);
        WWW www = new WWW(CreateUserURL, form);
    }
}

And This is my New PHP code below.

//Variables for the connection
    $servername = "localhost";
    $server_username =  "root";
    $server_password = "mysql";
    $dbName = "wizard2019";
    $saveuser = "INSERT INTO users (username, email, password, regdata)
            VALUES ('".$username."','".$email."','".$password."','".$regdata."')";
            
            
//Variable from the user    
    $username = $_POST["usernamePost"];
    $email = $_POST["emailPost"];
    $password = password_hash($_POST["passwordPost"], PASSWORD_DEFAULT);
    $regdata = $_POST["regdataPost"];
    //$username = "helder";
    //$email = "test email";
    //$password = "123456";
    //$regdata = "20201123";
    //Make Connection
    $conn = new mysqli($servername, $server_username, $server_password, $dbName);
    //Check Connection
    if(!$conn){
        die("Connection Failed. ". mysqli_connect_error());
    }
    
    $sql = $saveuser;
    $result = mysqli_query($conn ,$sql);
    
    if(!$result) echo "there was an error";
    else echo "Registration Sucessfull";

?>
  • 2
    Have SQL injection in mind and rather use prepared statements! – derHugo Nov 22 '20 at 13:25
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Nov 22 '20 at 14:42

1 Answers1

0

First of all WWW class has been obsolete. Look at this docs of unity here. Also you need to use 'Coroutine' to wait until you get a response from WWW. See the code below:

void Start () {
     string url = "http://www.example.com/";
     WWW www = new WWW(url);
     StartCoroutine(WaitForRequest(www));
 }

 IEnumerator WaitForRequest(WWW www)
 {
     yield return www;

     // check for errors
     if (www.error == null)
     {
         Debug.Log("Success: " + www.data);
     } else {
         Debug.Log("WWW Error: "+ www.error);
     }    
 }

Check this amazing tutorial of Unity with PHP MySQLi here if you really want to stick with your approach.

I recommend you to use UnityWebRequest to POST or GET data. See this official docs here.

For Example:**

  IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");

    using (UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}
Saad Anees
  • 1,255
  • 1
  • 15
  • 32