I’m trying to register users in my program and save the data in an online database. I have followed this tutorial:
but it’s outdated, so I have tried to make it work but I don’t know what is the error.
I have the script Registro.cs:
public class Registro : MonoBehaviour
{
public static string usuario = "", email = "";
private string contraseña = "", reContraseña = "", mensaje = "";
public Text txtUsuario, txtEmail, txtContraseña, txtReContraseña;
public InputField inputUsuario, inputEmail, inputContraseña, inputReContraseña;
public Button botonRegistrar;
void Start()
{
botonRegistrar.onClick.AddListener(ButtonRegistrarClicked);
}
void Update()
{
usuario = inputUsuario.text;
email = inputEmail.text;
contraseña = inputContraseña.text;
reContraseña = inputReContraseña.text;
}
void ButtonRegistrarClicked()
{
mensaje = "";
if(usuario == "" || email == "" || contraseña == "")
{
mensaje = "Por favor, rellena todos los campos";
print(mensaje);
}
else
{
if(contraseña == reContraseña)
{
WWWForm form = new WWWForm();
form.AddField("usuario", usuario);
form.AddField("email", email);
form.AddField("contraseña", contraseña);
UnityWebRequest www = UnityWebRequest.Post("http://linxdatos.000webhostapp.com/Registrar.php", form);
StartCoroutine(registrar(www));
}
else
{
mensaje += "Tu contraseña no coincide";
print(mensaje);
}
}
}
IEnumerator registrar(UnityWebRequest www)
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
print(www.downloadHandler.text);
Debug.Log("Form upload complete!");
}
}
}
Sorry for writing all the script here, but I don’t know where is the error. When I click the button botonRegistrar, it sends the data that you can see there to a free hosting database. I have written Debug.Log(www.error); to check if there is an error connecting to the database, but it says that it’s correct. I have tried to print(form.data); to check it and it prints System.Byte[ ], so maybe that’s the error?
Here is the php script:
<?php $usuario = $_POST['usuario'] ?? ''; $email = $_POST['email'] ?? ''; $contraseña = $_POST['contraseña'] ?? ''; $nombreservidor = "localhost"; $usuariodb = "id11649923_usuario"; $contraseñadb = "linx1234"; $nombredb = "id11649923_linxdb"; $conexion = new mysqli($nombreservidor, $usuariodb, $contraseñadb, $nombredb); if(!$conexion) { die("Conexión fallida.". mysqli_connect_error()); } $sql = "SELECT * FROM usuarios WHERE `usuario`='".$usuario."'"; $resultado = mysqli_query($conexion, $sql); if(mysqli_num_rows($resultado) == 0) { $query = "INSERT INTO `usuarios` ( `id` , `usuario` , `email` , `contraseña` ) VALUES ('' , '".$usuario."' , '".$email."' , '".$contraseña."') ; "; if ($query) { die("Usuario creado con éxito"); } else { die("Error: " . mysql_error()); } } else { die("El usuario ya existe"); } ?>Here it does the connection and insert the values in the database, but it’s inserting nothing.