I’m trying to set up my Account registration system to input data from unity to my database, and so far i have been able to do so through the Inspector, now I want to be able to do this with my UI that i made in Unity, what do I need to do? (P.S. this is my first time posting and i am also a beginner so excuse me if i may not follow some rules and what not)
here is the code i used to input data to my PhpMyAdmin database through the inspector in Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataInserter : MonoBehaviour
{
public GameObject inputUserName;
public GameObject inputEmail;
string CreateUserURL = "http://localhost/balikaral/insertAccount.php";
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) CreateUser(inputUserName, inputEmail);
}
public void CreateUser(string username, string email)
{
WWWForm form = new WWWForm();
form.AddField("usernamePost", username);
form.AddField("emailPost", email);
WWW www = new WWW(CreateUserURL, form);
}
}
2
Answers
you shouldn’t do it via PhpMyAdmin, it’s a mysql tool for manage mysql thing, I assume you want to create a player’s account for your game .
there are two ways to do what you want
1. create a webApi site that accept Http post (recommand), the webapi will have connect to your mysql database , and do the CRUD(Create,Read,Update,Delete) thing
2. create a mysql connection directly to your mysql database, and insert your data to your mysql table. (it’s unsafe)
Neigher way, you need to know about the
SQL
language.You simply have to get the input from your input fields
InputField.text
I recommend to directly use
InputField
fields so you don’t need theGetComponent
calls.And reference the
CreateUser
method in the Button’sonClick
event.Note however that
WWW
is obsolete and you should rather useUnityWebRequest.Post