-1

for my unity game i am trying to make an end score screen where at the end of a level the players imputed name and score are put into a database. i have linked all the game objects correctly but the script doesn't even give an error code and i cant see what i have missed. i have now tried without the adding the score (only adding a imputed name) but i still get the same issue

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class AddScore : MonoBehaviour
{
    public InputField nameField;
    public GameObject Time;
    public GameObject ScoreText;

    public Button submitScore;

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

    IEnumerator Register()
    {
        WWWForm form = new WWWForm();
        form.AddField("name", nameField.text);
        UnityWebRequest www = UnityWebRequest.Get("https://localhost/sqlconnect/addscore.php");
        yield return www.SendWebRequest();
        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }
        else
        {
            Debug.Log("Score added");
            UnityEngine.SceneManagement.SceneManager.LoadScene(0);
        }
    }

    public void VerifyInputs()
    {
        submitScore.interactable = (nameField.text.Length <= 8);
    }
}

i can't tell if it is an issue with the php or the c# code so i put them both up here

<?php

    $con = mysqli_connect('localhost', 'root', 'password', 'Leaderboard');
    
    if (mysqli_connect_error())
    (
        echo"1; Connection failed";
        exit()
    )
    
    $username = $_POST["name"];
    
    $insertuserquery = "INSERT INTO addscore(username) VALUES ('" . $username . "');";
    mysqli_query($con, $insertuserquery) or die("4: Insert addscore query failed");
    
    echo("0")
?>
FaithlessS
  • 17
  • 5
  • `GET` != `POST` .. your php expects the `POST` protocol you are using `GET` without sending the form you created also ;) – derHugo Dec 16 '20 at 16:07
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 17 '20 at 19:03

1 Answers1

1

You are doing a GET request without any body:

  UnityWebRequest www = UnityWebRequest.Get("https://localhost/sqlconnect/addscore.php");

You should be doing a POST request with the username parameter in the body

Unity POST data

Example Code:

        WWWForm form = new WWWForm();
        form.AddField("name", "myUserName");

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

        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
            }
        }
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61