Using WWW to add Data to MySQL Database? Unable to connect?

Hey Guys,

I just following a Guide right now to create a Login/CreateAccount Script for my Testproject.
I did everything like in the Tutorial but I stuck there with

Here’s the Script I use:

using UnityEngine;
using System.Collections;

public class Login : MonoBehaviour {
#region Variables
    //Static Variables
    public static string Username = "";
    public static string Email = "";
    public static string Password = "";

    //Public Variables
    public string CurrentMenu = "Login";

    //Private Variablles
    private string CreateAccountUrl = "http://*****************/createaccount.php";
    private string LoginUrl = "";
    private string ConfirmPass = "";
    private string CUsername = "";
    private string CEmail = "";
    private string CPassword = "";

    //GUI Test section
    float X;
    float Y;
    float Width;
    float Height;
#endregion

    void Start () {
 
    }//END

    //Main GUI Function
    void OnGUI(){
        X = (Screen.width / 4);
        Y = (Screen.height / 4);
        Width = X*2;
        Height = Y*2;
        if(CurrentMenu == "Login"){
            LoginGUI();
        }else if(CurrentMenu == "CreateAccount"){
            CreateAccountGUI();
        }
    }//END OnGUI

#region Custom Methods
    //GUI for Login
    void LoginGUI(){
        GUI.Box(new Rect(X , Y, Width, Height), "Login");
        GUI.Label(new Rect(X*1.50f, Y+25, 200, 25), "E-Mail:");
        Email = GUI.TextField(new Rect(X*1.50f, Y+45, 200, 25), Email);
        GUI.Label(new Rect(X*1.50f, Y+70, 200, 25), "Passwort:");
        Password = GUI.TextField(new Rect(X*1.50f, Y+90, 200, 25), Password);
        //Create Account Button
        if(GUI.Button(new Rect(X+50, Y+150, 150, 25), "Account erstellen")){
            CurrentMenu = "CreateAccount";
        }
        if(GUI.Button(new Rect(X+225, Y+150, 150, 25), "Einloggen")){

        }
    }//End login GUI

    //GUI for Account creation
    void CreateAccountGUI(){
        GUI.Box(new Rect(X , Y, Width, Height+20), "Account erstellen");
        GUI.Label(new Rect(X*1.50f, Y+25, 200, 25), "Username:");
        CUsername = GUI.TextField(new Rect(X*1.50f, Y+45, 200, 25), CUsername);
        GUI.Label(new Rect(X*1.50f, Y+70, 200, 25), "E-Mail:");
        CEmail = GUI.TextField(new Rect(X*1.50f, Y+90, 200, 25), CEmail);
        GUI.Label(new Rect(X*1.50f, Y+115, 200, 25), "Passwort:");
        CPassword = GUI.TextField(new Rect(X*1.50f, Y+135, 200, 25), CPassword);
        GUI.Label(new Rect(X*1.50f, Y+160, 200, 25), "Passwort bestätigen:");
        ConfirmPass = GUI.TextField(new Rect(X*1.50f, Y+180, 200, 25), ConfirmPass);

        //Create Account Button
        if(GUI.Button(new Rect(X+50, Y+220, 150, 25), "Account erstellen")){
            if(ConfirmPass == CPassword){
                StartCoroutine("CreateAccount");
            }else{
            //    StartCoroutine();
            }
        }//END Create Account
        if(GUI.Button(new Rect(X+225, Y+220, 150, 25), "Zurück")){
            CurrentMenu = "Login";
        }
    }//End of Account Creation
#endregion

#region CoRoutines
    IEnumerator CreateAccount(){
        //Send to PHP
        WWWForm Form = new WWWForm();
        Form.AddField("Username", CUsername);
        Form.AddField("Email", CEmail);
        Form.AddField("Password", CPassword);
        WWW CreateAccountWWW = new WWW(CreateAccountUrl, Form);
        //wait for PHP to send
        yield return CreateAccountWWW;
        if(CreateAccountWWW.error != null){
            Debug.LogError("Cannot connect to Account Creation");
        }else{
            string CreateAccountReturn = CreateAccountWWW.text;
            if(CreateAccountReturn == ""){
                Debug.Log("Success; Account created");
                CurrentMenu = "Login";
            }
        }
    }//END Create Account
#endregion

}//End Class

and this is the PHP Script I use:

<?php
//Email and Password
$Username = $_REQUEST["Username"];
$Email = $_REQUEST["Email"];
$Password = $_REQUEST["Password"];

//PHP Only
$Hostname = "my-serverip.";
$DBName = "UnityGame";
$UserP = "UnityGame";
$PasswordP = "[HIDDEN]";

mysql_connect($Hostname, $UserP, $PasswordP) or die("Can't connect to DB");
mysql_select_db($DBName) or die ("Can't connect to DB");

if(!$Email || !$Password){
    echo "Empty";
}else{
    $SQL = "SELECT * FROM accounts WHERE Email ='" . $Email . "'";
    $Result = @mysql_query($SQL) or die ("DB Error");
    $Total = mysql_num_rows($Result);
        if($Total == 0){
            $insert = "INSERT INTO 'accounts' ('Username', 'Email', 'Password') VALUES ('" . $Username . "', '" . $Email . "', MD5('" . $Password "'),0)";
            $SQL1 = mysql_query($insert);
            echo "Success";
        }else{
            echo "AlreadyUsed";
        }
    }
}//END MAIN ELSE
 
//Close MySQL
mysql_close();
?>

both are still unfinished but at this state it should be able to connect to the server and create a Account.
Any Idea why I can’t connect to the server?

Have you looked at the error property on the WWW instance? What message does that contain?

This is what I got.
Any Idea what I can do?

I saw that, but that string (“Cannot connect to Account Creation”) was something your code wrote:

if(CreateAccountWWW.error != null){
            Debug.LogError("Cannot connect to Account Creation");
        }

Try changing it to this:

if(CreateAccountWWW.error != null){
            Debug.LogError("Cannot connect to Account Creation: " + CreateAccountWWW.error);
        }

The error property is a string that may contain additional error information that will tell you what failed.

Cannot connect to Account Creation: 500 Internal Server Error

This is weird and I don’t know why I got a 500 Internal Server Error o.o