- Home /
WWWForms and sending data to database dont work
Hello,following code dont work to talk to the database! Just nothing happens! When i click on the Button the StartCoroutine will be executed but the script dont talk to the database! Anyone any ideas? Thank you!
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CreateAccountManager : MonoBehaviour {
//Static Variables
//Variablen für LoginFormular
public static string accountEmail = "";
public static string accountEmailConfirm = "";
public static string accountPassword = "";
public static string accountPasswordConfirm = "";
//Public Variables
//Private Variables
private string createAccountUrl = "http://127.0.0.1/CreateAccount/Test1.php";
//FormInputs
[SerializeField]
private InputField fldAccountUsername;
[SerializeField]
private InputField fldAccountUsernameConfirm;
[SerializeField]
private InputField fldAccountPassword;
[SerializeField]
private InputField fldAccountPasswordConfirm;
[SerializeField]
private Text txtErrorMessage;
// Use this for initialization
void Start () {
fldAccountUsername.contentType = InputField.ContentType.EmailAddress;
txtErrorMessage.text = "";
}
// Update is called once per frame
void Update () {
accountEmail = fldAccountUsername.text;
accountEmailConfirm = fldAccountUsernameConfirm.text;
accountPassword = fldAccountPassword.text;
accountPasswordConfirm = fldAccountPasswordConfirm.text;
}
public void CreateAccountClicked ()
{
if( accountPasswordConfirm == accountPassword && accountEmailConfirm == accountEmail )
{
StartCoroutine ("CreateUserAccount");
}
else
{
txtErrorMessage.text = "Error: Input dont match!";
}
}
IEnumerator CreateUserAccount()
{
WWWForm Form = new WWWForm ();
Form.AddField ("UserEmail", accountEmail);
print (accountEmail);
Form.AddField ("UserPassword", accountPassword);
WWW CreateAccountWWW = new WWW (createAccountUrl, Form);
//Wait for PHP-File!
yield return CreateAccountWWW;
if (CreateAccountWWW.error != null)
{
Debug.LogError ("Cannot Connect to Create Account!");
txtErrorMessage.text = "Error: Cannot Connect to Create Account!";
}
else
{
string CreateAccountReturn = CreateAccountWWW.text;
if(CreateAccountReturn == "Success" )
{
Debug.Log ("Success: Account Created!");
}
}
}
}
edit(moved from answer)
This is the PHP-File to talk to the database:
<?php
//Email and Password
@$UserEmail = $_REQUEST["UserEmail"];
@$UserPassword = $_REQUEST["UserPassword"];
//PHP Only
$servername = "localhost";
$serverUserName = "root";
$serverPassword = "";
$dbName = "login";
$conn = new mysqli($servername, $serverUserName, $serverPassword, $dbName);
if(!$UserEmail || !$UserPassword){
echo"Empty fields!";
} else {
$SQL = "SELECT * FROM accounts WHERE Email = '" . $UserEmail ."'";
$Result = @mysqli_query($conn, $SQL) or die ("Database Error");
$Total = mysqli_num_rows($Result);
if($Total == 0){
$insert = "INSERT INTO 'accounts' ('Email', 'Password') VALUES ('" . $UserEmail . "'. MD5('" . $UserPassword . "')";
$SQL1 = mysql_query($insert);
echo "Success";
} else {
echo "AlreadyUsed";
}
}
//Close Mysq
//mysql_close();
?>
Can anyone help? Thx!
Any Ideas? The PHP File works if i open the URL in the browser, i get the echo "Empty fields"! If i give the variables in the PHP File a string data type (username and password) and i open the file in the browser, i get the echo "Success"! But if i click on the button in my "Game" in Unity, nothing happens! Can anyone help? Thank you!
Try using password_hash($passwordvariable ,PASSWORD_DEFAULT); the second parameter is the strength of the encryption. Can use BCRYPT with it.
Then use if(password_verify($passwordvariable, $row['Password'])) to check during login.
Answer by Bunny83 · Jul 15, 2016 at 09:02 AM
Change this:
if(CreateAccountReturn == "Success" )
{
Debug.Log ("Success: Account Created!");
}
to this:
if(CreateAccountReturn == "Success" )
{
Debug.Log ("Success: Account Created!");
}
else
{
Debug.Log ("Request sent but it returned: " + CreateAccountReturn);
}
And see what you get back from your script. Some server setups do not pass POST variables inside $_REQUEST. Have you tried $_POST instead? If $_POST also doesn't work there could be some setting in your servers config that prevent post variables from getting through. In this case you could also switch to URL parameters instead.
Hi thank you for your help!
The Problem is, my Code will be executed until this line:
string CreateAccountReturn = CreateAccountWWW.text;
Debugging get me these informations:
Cannot Connect to Create Account! UnityEngine.Debug:LogError(Object) c__Iterator0:$$anonymous$$oveNext() (at Assets/folder/folderAssets/Scripts/CreateAccount$$anonymous$$anager.cs:71)
UnityEngine.WWW UnityEngine.Debug:LogError(Object) c__Iterator0:$$anonymous$$oveNext() (at Assets/folder/folderAssets/Scripts/CreateAccount$$anonymous$$anager.cs:72)
I also tried $_POST in the PHP Script, nothing helped! The PHP File is on localhost. I simulate the Server with xampp! Apache and $$anonymous$$YSQL are running!
Thank you!
Sorry for the late response. I had this question still open in my browser (besides 150 other) and currently cleaning up a bit ^^. Well The WWW class has the "error" text property for a reason. If the request fails it tells you the error that the server returned. You might want to output it's content in case it's not null. So try:
if (CreateAccountWWW.error != null)
{
Debug.LogError ("Cannot Connect to Create Account!");
Debug.LogError ("server returned: " + CreateAccountWWW.error);
txtError$$anonymous$$essage.text = "Error: Cannot Connect to Create Account!"+ "\nSerer returned: " + CreateAccountWWW.error;
}
And see what you get.
Your question however is misleading then. You said
Just nothing happens
That's not true when you actually get an error. The only case where you would see "nothing" is when the request has reached the server without problems (error == null) but the server doesn't return "success". However in your case your server returns an error.
Answer by IanSantos · Aug 10, 2017 at 03:55 PM
Same problem and can't find the solution. I've been fixing this for 3days straight. I can send data from inputfields to database but if it is on localhost server and when I used webhosting for free, now I can't send data to online database. btw I'm using 000webhost for free.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System;
public class RegisterInput : MonoBehaviour {
string CreateClienturl = "https://192.127.0.565.000webhostapp.com/main/savesupplier.php";
public InputField Namefield;
public InputField Addressfield;
public InputField TelephoneNumberfield;
public InputField MobileNumberfield;
public InputField Emailfield;
public string Name;
public string Address;
public string Telnum;
public string Mobnum;
public string Email;
public GameObject RegisterObject;
public void click_btn ()
{
Name = Namefield.text;
Address = Addressfield.text;
Telnum = TelephoneNumberfield.text;
Mobnum = MobileNumberfield.text;
Email = Emailfield.text;
CreateClient (Name, Address, Telnum, Mobnum, Email);
RegisterObject.SetActive (true);
StartCoroutine (LateCall());
}
IEnumerator LateCall()
{
yield return new WaitForSeconds (2);
RegisterObject.SetActive (false);
}
public void CreateClient(string name, string address, string telnum, string mobnum, string email)
{
WWWForm form = new WWWForm ();
form.AddField ("name", name);
form.AddField ("address", address);
form.AddField ("telnum", telnum);
form.AddField ("mobnum", mobnum);
form.AddField ("email", email);
WWW www = new WWW(CreateClienturl, form);
Debug.Log("Success!");
Application.LoadLevel ("Register");
}
}
here is my code
Your answer
![](https://koobas.hobune.stream/wayback/20220612084324im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help saving Voxel Terrain between scenes 2 Answers
Why PlayerPrefs not working although I use "HasKey" and Save() on android? 0 Answers
Storing data on a server to be used for player currency in a multiplayer game 1 Answer
How do I go about using a server to store and retrieve data? 0 Answers
Modifying Json file values 2 Answers