Pass input values to database,Pass my Input Fields to database
Hello! I'm new to unity and i'm trying to to create a simple register system.I created a database with usernames and passwords and i want to pass input values from unity to database. My input fields(UI -> Input Field) are three: username, password and confirm password. But when i call my function CreateUser( string, string) i get this error:
NullReferenceException: Object reference not set to an instance of an object Register.Update () (at Assets/Scripts/Register.cs:32)
I don't understand what is wrong?
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Register : MonoBehaviour {
string CreateUserURL = "localhost/login.php";
public InputField username;
public InputField password;
public InputField confPassword;
public string username1;
public string password1;
public string confPassword1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
username1 = username.GetComponent<InputField>().text;
password1 = password.GetComponent<InputField>().text;
Debug.Log ("your username is " + username1);
CreateUser(username1, password1);
}
}
public void CreateUser(string username1, string password1){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username1);
form.AddField("passwordPost", password1);
WWW www = new WWW(CreateUserURL, form);
Debug.Log("Done");
}
}
Answer by tormentoarmagedoom · Jul 11, 2018 at 06:59 AM
Good day.
You need to learn how to solve this problem. Null exception errors are the most common errors.. Is because some variable is not assigned, (is null) when trying to access it...
Now its happening in the line 32 as the ewrror says.
so its in
username1 = username.GetComponent<InputField>().text;
Use a Debug.Log or Debug the code while running and you will see some of the variables does not exist or are null (maybe username does not exist or does not have a component calles InputField)
Go spet by step, cheking everything and you will find by your own.
PD: As this is your 1st post, i will let it here, but we normally delete all posts about Null reference problems, because there are very simple to solve, and you have 10000 posts about the same.
Bye!
Answer by isiaaa · Jul 19, 2018 at 04:35 PM
Hello again!I followed your advice and the problem now is different. My code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Text.RegularExpressions;
public class Register : MonoBehaviour {
string CreateUserURL = "localhost/users/login.php";
public InputField username;
public InputField password;
public InputField confPassword;
public string username1;
public string password1;
public string confPassword1;
// Use this for initialization
void Start () {
Debug.Log("Start");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
username1 = username.GetComponent<InputField>().text;
password1 = password.GetComponent<InputField>().text;
Debug.Log ("your username is " + username1);
CreateUser(username1, password1);
}
}
public void CreateUser(string username1, string password1){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username1);
form.AddField("passwordPost", password1);
WWW www = new WWW(CreateUserURL, form);
Debug.Log("Done");
}
} My php code is:
<?php
$servername = "localhost";
$server_username = "root";
$server_password = "";
$dbName = "myusers";
$username = $_POST['usernamePost'];
$password = $_POST['passwordPost'];
//Make Connection
$conn = new mysqli($servername, $server_username, $server_password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed.".mysqli_connect_error());
}
$sql = "INSERT INTO users(username, password)
VALUES ('".$username."','".$password."')";
$result = mysqli_query($conn, $sql);
if(!$result) echo "there was an error";
else echo "ok";
?>
In unity i get the message 'Done' but in my database my records are increasing without the values of username and password (they are blank). I can't understand what's wrong and any help would be usefull.
Thanks again!
Your answer
Follow this Question
Related Questions
using databse for "enter name" system 0 Answers
Inventory that loads data from an XML 0 Answers
how to define a user id securely 0 Answers
Get Other Players information (those who are installed) ? 0 Answers
How to setup AWS dynamoDB that I can CRUD (create, read, update, delete) data by Unity? 0 Answers