Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by strudi1986 · Jul 06, 2016 at 05:53 PM · databasewwwformdata storage

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!



Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image strudi1986 · Jul 15, 2016 at 08:32 AM 0
Share

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!

avatar image Gilead7 · Nov 20, 2017 at 06:51 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image strudi1986 · Jul 16, 2016 at 11:19 AM 0
Share

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!

avatar image Bunny83 strudi1986 · Jul 27, 2016 at 09:00 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges