Problem with uploading a username and password onto a database.

Hi, I’m trying to follow the following Unity tutorial:

on how to create a register/login system on a database for Unity. I have followed the tutorial to the point where he starts to populate his database with “players” which are made from a username text field and a password text field (the password is hashed.) When I run the code in Unity, it tells me Form Upload Complete! which is the message for a successful transmission but when I go onto my database (MYSQL database using MAMP) the database is still completely empty. Does anyone know how I can solve the problem so that when I click the Register button on my game, it actually populates my database as opposed to just telling me it works? Thanks.

Unity Code:

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

public class Registration : MonoBehaviour
{
    public InputField nameField;
    public InputField passwordField;
   
    public Button acceptSubmissionButton;

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

    IEnumerator Register()
    {
        WWWForm form = new WWWForm();
        form.AddField("username", nameField.text);
        form.AddField("password", passwordField.text);
        UnityWebRequest www = UnityWebRequest.Post("http://localhost/sqlconnect/register.php", form); // The file location for where my .php file is.
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form Upload Complete!");
            }       
    }

    public void Validation()
    {
        acceptSubmissionButton.interactable = nameField.text.Length >= 7  && passwordField.text.Length >= 8;
    }
}

My .php code:

<?php $con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); // check for successful connection. if (mysqli_connect_errno()) { echo "1: Connection failed"; // Error code #1 - connection failed. exit(); } $username = $_POST["username"]; $password = $_POST["password"]; // check for if the name already exists. $namecheckquery = "SELECT username 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) > 0) { echo "3: Name already exists"; // Error code #3 - name exists already. exit(); } $salt = "\$5\$rounds=5000\$" . "safetycharacters" . $username . "\$"; $hash = crypt($password, $salt); $insertuserquery = "INSERT INTO players (username, hash, salt) VALUES ('" . $username . "', '" . $hash . "', '" . $salt . "');"; mysqli_query($con, $insertuserquery) or die("4: Insert player query failed"); // Error code #4 - insert query failed. echo("0"); ?>

The first steps to debugging and service problems are generally:

  1. check the server logs for errors and to see if the endpoint is even being reached

  2. try the entire transaction using curl at the command line to make sure it actually works.

If you can’t make it work with curl, you won’t make it work with Unity! Go fix the server code; that’s not a Unity problem.

if it works with curl, put a proxy on the line (like Charles) and see what it sends. Then run it in Unity and compare. Often times it will just be some minor encoding issue or mime-type stuff, perhaps a property or field you need to add to your form.

2 Likes

Thanks, for the suggestions, I will give it a try and post what I can find out about it. Thanks!

I fixed the problem! My problem was that on line 3: the $db I was connecting to was the one used in the tutorial and not my actual database. This mean that it couldn’t connect. Now it makes a field in my table is intended :slight_smile:

1 Like

The reason may be error #1 or error #3. You see, all errors posted from server using die() call. This implies sending back a error code, but those two prints string and quits without sending error code, so i believe 200 OK will be sent back even in case of server side error. Replace echo with die for all error cases in the script and try again.

1 Like

A few notes. You are aware that no one else will be able to write to your database when the connection is localhost. You’ll need to place your db on a public host like AWS, but you probably already know this. Also, you should never open a direct client connection like this. If your game becomes popular and you have over 100 users, your db access will likely crash as each user will try to create a separate connection. Instead you’ll want to use a proper web service REST API or similar, and use a single connection or connection pooling.

1 Like

Thank you all for your help. @JeffDUnity3D The database is being made to satisfy criteria for my Computer Science A-Level coursework meaning that the program only needs to run on a single computer which is why it is working in this “interesting” way of joining! Also, the testing for the coursework only requires a single connection from one user to store and download data. I plan to try and make the coursework project into a game after the coursework is completed, but I think I will likely scrap the leaderboard entirely or follow your advice to make it public. Thank you.

1 Like

Hi, I make a scene according to this tutorials and create database in my server. The WWWform go correctly and the debug log show " Successful" but in table, it create a row with ID and nothing fill the datas like username, password, etc in my database table.