0

Unity Code

Scorestr not passing to php variables only $qfName is being saved in query. I am new to both php and unity. maybe explained or simple answers would be appreciated. :)

public GameObject gamectrl;

    public void CallRegister()
    {       
        StartCoroutine(Register());
    }
    IEnumerator Register()
    {     
        List<IMultipartFormSection> wwwForm = new List<IMultipartFormSection>();

        int var = gamectrl.GetComponent<GameController>().Score;
        string scorestr  = var.ToString();
        //UnityEngine.Debug.Log(abc);

        wwwForm.Add(new MultipartFormDataSection("score", scorestr));
        UnityWebRequest www = UnityWebRequest.Post("http://localhost/testphp/HighScore.php", wwwForm);
        yield return www.SendWebRequest();

    }

php code

<?php   
include 'connectivity.php'; 

$_POST["score"] = 1;

$unityscore =  $_POST["score"] ;

$qfName = 'queryrunning';
$insertscorequery  = "INSERT INTO highscore (FirstName, HighScore ) VALUES ('$qfName' , '$unityscore' )";
mysqli

_query($con,$insertscorequery);
    ?>
  • Why is the first thing you do in PHP overwrite the value of `$_POST["score"] = 1;` ? Also note that this PHP is wide open for [**SQL Injection**](https://www.w3schools.com/sql/sql_injection.asp) Rather use a [prepared statement](https://www.w3schools.com/php/php_mysql_prepared_statements.asp) – derHugo May 23 '20 at 10:48
  • If i ignore that and write $unityscore = $_POST["score"] ; direcy. lt will show an undefined index error msg – Fahad Nadeem May 23 '20 at 11:17
  • Can you try [this](https://stackoverflow.com/a/48780251/7111561)? – derHugo May 23 '20 at 11:26
  • I tried it ! but I think that $_POST["score"] = 1; maybe interfering. I saw a lot of codes they all are working without this line. Why am I the only one getting this error ? – Fahad Nadeem May 25 '20 at 00:42
  • I am sorry I am stupid AF. Please dont mind me. – Fahad Nadeem May 25 '20 at 00:44

0 Answers0