I’ve set up a MySQL database, PHP script, and a C# script to try and setup a registration service. The database seems to be working correctly. When I access my PHP script through my browser it works correctly creating a username and password in my MySQL database. When I run my C# script through unity however it does not create a username or password at all. I cannot find what the issue would be if someone could help me out id be very appreciative.
PHP code:
<?php
$servername = “localhost”;
$serverusername = “”;
$serverpassword = “”;
$serverdatabasename = “accountdb”;
$username = $_POST["usernamePost"];
$password = $_POST["passwordPost"];
//Make Connection
$connection = new mysqli($servername, $serverusername, $serverpassword, $serverdatabasename);
//Check Connection
if(!$connection){
die("Connection failed. ". mysqli_connect_error());
}
$check = "SELECT * FROM accounts WHERE `username`='".$username."'";
$result = mysqli_query($connection , $check);
$numrows = mysqli_num_rows($result);
if ($numrows == 0)
{
$pass = md5($password);
$ins = "INSERT INTO accounts (`username` , `password`) VALUES ('".$username."' , '".$password."')";
$result = mysqli_query($connection , $ins);
if ($result)
die ("Successfully Created User!");
else
die ("Error: ");
}
else
{
die("User already exists!");
}
?>
C# Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DataInserter : MonoBehaviour {
public InputField inputUsername;
public InputField inputPassword;
string createUserURL = "http://localhost/NewUser.php";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) Register(inputUsername.text, inputPassword.text);
}
public void Register(string username, string password)
{
StartCoroutine(CreateUser(username, password));
}
IEnumerator CreateUser(string username, string password)
{
WWWForm form = new WWWForm();
form.AddField("usernamePost", username);
form.AddField("passwordPost", password);
WWW www = new WWW(createUserURL, form);
Debug.Log(www.error);
Debug.Log(www.text);
yield return www;
if (www.error != null)
{
Debug.Log("Something went wrong");
}
}
}