Search Unity

Save data to mysql

Discussion in 'Scripting' started by prezio, Mar 18, 2019.

  1. prezio

    prezio

    Joined:
    Dec 27, 2014
    Posts:
    23
    This is my script in php:

    $con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); //la primera var es la posicion relativa de la ddbb, nombre y paswword en las sgtes

    //check that conection happenned
    if(mysqli_connect_errno()) //check or si pasaron algunos errores, si es true esq hubieron errores.
    {
    echo "1: conection failed"; // error code #1 = connection failed
    exit();
    }


    $username = $_POST["name"];
    $newscore = $_POST["score"];

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


    $namecheck = mysqli_query($con, $namecheckquery) or die("2: name check query failed"); // error code #2 = name check query failed
    if(mysqli_num_rows($namecheck) != 1)
    {
    echo "5: Either no user with name or more than one"; // error code #5 number of names maching !=1
    exit();
    }

    $updatequery = "UPDATE players SET score = ".$newscore." WHERE username = '".$username."' ";
    mysqli_query($con, $updatequery) or die ("7: Save query failed");

    echo "0";


    This my script in C#:

    public Text playerDisplay;
    public Text scoreDisplay;
    // Start is called before the first frame update
    void Awake()
    {

    if(DBManager.username == null)
    {
    UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    }
    playerDisplay.text = "Player : " + DBManager.username;
    scoreDisplay.text = "score: " + DBManager.score;



    public void CallSaveData()
    {
    StartCoroutine(SavePlayerData());
    }

    IEnumerator SavePlayerData()
    {
    WWWForm form = new WWWForm();
    form.AddField("name", DBManager.username);
    form.AddField("score", DBManager.score); //
    WWW www = new WWW("http://localhost:8888/sqlconnect/savedata.php", form);
    yield return www;
    if(www.text == "0")
    {
    Debug.Log("Game Saved");



    }
    else
    {
    Debug.Log("Save failed. Erro # " + www.text);
    }

    DBManager.LogOut();
    SceneManager.LoadScene(0);

    }

    public void IncreaseScore()
    {
    DBManager.score++;
    scoreDisplay.text = "score: " + DBManager.score;
    }


    I am getting game saved but when I login again the score is still '0' and the score also did not update in my table and I don't know why.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    This is not forum about PHP.
    Also use correctly forum Code Tagging.
     
    Joe-Censored likes this.
  3. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    I am not seeing anything wrong. Debugging your code should come up with the answer pretty quickly. Also you should never directly access super globals ( your $_POST). You should filter and sanitize your input.