Search Unity

HELP! cant save player position with php to sql!

Discussion in 'Scripting' started by SinopGerze, Jul 12, 2021.

  1. SinopGerze

    SinopGerze

    Joined:
    Jul 5, 2021
    Posts:
    1
    hello i need help cant save my player position to sql with php

    C# code:

    public void OnQuit()
    {
    StartCoroutine(SavePostion());
    }
    public IEnumerator SavePostion()
    {
    WWWForm form = new WWWForm();
    form.AddField("VectorX", transform.position.x.ToString("0.00"));
    form.AddField("VectorY", transform.position.y.ToString("0.00"));
    form.AddField("VectorZ", transform.position.z.ToString("0.00"));


    WWW www = new WWW("http://localhost/python/login.php", form);
    yield return www;
    if(www.text.Contains ("5a"))
    {
    Debug.Log("Saved!");
    }
    }

    php code:
    <?php
    $serverName = "localhost";
    $id = "root";
    $pw ="";
    $db = "masterdb";
    $LoginUser = $_POST['Username'];
    $LoginPassword = $_POST['Password'];
    $VectorX = $_POST['VectorX'];
    $VectorY = $_POST['VectorY'];
    $VectorZ = $_POST['VectorZ'];
    $conn = mysqli_connect($serverName, $id,$pw,$db);
    $sql = "SELECT level, exp, username, password ,money,vectorX,vectorY,vectorZ FROM userdb WHERE username LIKE '$LoginUser' AND password LIKE '$LoginPassword'";
    $result = $conn->query($sql);
    if($result->num_rows >0)
    {
    echo "1f";
    echo '-';
    while($row = $result->fetch_assoc())
    {
    echo $row['money'];
    echo '-';
    echo $row['level'];
    echo '-';
    echo $row['vectorX'];
    echo '-';
    echo $row['vectorY'];
    echo '-';
    echo $row['vectorZ'];
    echo '-';
    echo $row['exp'];
    echo '-';
    }
    }
    else
    {
    echo "1d";
    }
    $sql2 = "UPDATE userdb SET vectorX = '$VectorX' WHERE username ='$LoginUser' ";
    if($conn->query($sql2) === TRUE)
    {
    echo "5a";
    }
    ?>
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Since Unity doesn't run PHP, this means you're contacting another server.

    To do so you will need UnityWebRequest().

    Anytime you are doing networking, ALWAYS START HERE:

    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

    If this is your first game, don't even THINK about doing networking. Finish about a dozen or so regular simple games (think Angry Birds or Flappy Birds) until you are completely at home with Unity before you attempt something as complicated as networking.