Search code examples
phpunity-game-engineftphostingdirectadmin

DirectAdmin mysql connect to unity game problem


I have writen a script to collect email and score from a game and send it to a database. so far while developing i have used MAMP and everything worked fine. now i uploaded the build to direct admin , set up the database on phpmyadmin, changed the database name password etc in my php file but it doesnt work! i have put all the files in the same folder in ftp ( unity build files and a sqlconnect folder that has the php script inside) but yea it doesnt update the database.

this is the database.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Database : MonoBehaviour
{
public InputField emailField;
public InputField inputFieldRef;   
public Button submitButton;
// Start is called before the first frame update
public void CallSignup()
{
    StartCoroutine(Signup());
}

IEnumerator Signup()
{
    WWWForm form = new WWWForm();
    form.AddField("email",emailField.text);
    emailField.text = string.Empty;
    form.AddField("score",GameManager.score);
    UnityWebRequest www = UnityWebRequest.Post("http://localhost/sqlconnect/signup.php",form);
    yield return www.SendWebRequest();
    
    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
    }
}
public void VerifyInput()
{
    submitButton.interactable = (emailField.text.Length >= 8);
}
}

and here is my php file

<?php

$servername = "localhost";
$username = "database username";
$password = "password";
$dbname = "database";

$conn = mysqli_connect($servername,$username,$password,$dbname);

if (mysqli_connect_errno())
{
    echo "1: Connection Failed";
    exit();
}

$email = msqli_real_escape_string($conn, $_POST["email"];
$emailclean = filter_var($email,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | 
FILTER_FLAG_STRIP_HIGH);
$score = $_POST["score"];

$updatequery = "UPDATE testscore SET score='$score' WHERE email ='$email'";
mysqli_query($conn , $updatequery) or die("FAILED ".__LINE__." : ".mysqli_error($conn));

echo "0";

?>

Solution

  • Once again after running the inspector I saw that because I didn't have a wildard DNS open it couldn't connect and show. So I added the header

    header('Access-Control-Allow-Origin: *');
    
    header('Access-Control-Allow-Methods: GET, POST');
    
    header("Access-Control-Allow-Headers: X-Requested-With");
    

    And I can see everything OK now