Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why it doesn't save data on the online database?

Discussion in 'Scripting' started by FVelasco, Jan 3, 2020.

  1. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    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:

    Code (CSharp):
    1. public class Registro : MonoBehaviour
    2. {
    3.     public static string usuario = "", email = "";
    4.     private string contraseña = "", reContraseña = "", mensaje = "";
    5.  
    6.     public Text txtUsuario, txtEmail, txtContraseña, txtReContraseña;
    7.     public InputField inputUsuario, inputEmail, inputContraseña, inputReContraseña;
    8.     public Button botonRegistrar;
    9.  
    10.     void Start()
    11.     {
    12.         botonRegistrar.onClick.AddListener(ButtonRegistrarClicked);
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         usuario = inputUsuario.text;
    18.         email = inputEmail.text;
    19.         contraseña = inputContraseña.text;
    20.         reContraseña = inputReContraseña.text;
    21.     }
    22.  
    23.     void ButtonRegistrarClicked()
    24.     {
    25.         mensaje = "";
    26.  
    27.         if(usuario == "" || email == "" || contraseña == "")
    28.         {
    29.             mensaje = "Por favor, rellena todos los campos";
    30.             print(mensaje);
    31.         }
    32.         else
    33.         {
    34.             if(contraseña == reContraseña)
    35.             {
    36.                 WWWForm form = new WWWForm();
    37.                 form.AddField("usuario", usuario);
    38.                 form.AddField("email", email);
    39.                 form.AddField("contraseña", contraseña);
    40.  
    41.                 UnityWebRequest www = UnityWebRequest.Post("http://linxdatos.000webhostapp.com/Registrar.php", form);
    42.                 StartCoroutine(registrar(www));
    43.             }
    44.             else
    45.             {
    46.                 mensaje += "Tu contraseña no coincide";
    47.                 print(mensaje);
    48.             }
    49.         }
    50.     }
    51.  
    52.     IEnumerator registrar(UnityWebRequest www)
    53.     {
    54.         yield return www.SendWebRequest();
    55.         if (www.isNetworkError || www.isHttpError)
    56.             {
    57.                 Debug.Log(www.error);
    58.             }
    59.             else
    60.             {
    61.                 print(www.downloadHandler.text);
    62.                 Debug.Log("Form upload complete!");
    63.             }
    64.     }
    65. }
    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.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    I suggest you always use same Unity version as per tutorial.
     
  3. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    Yeah I know, that's why I have used UnityWebRequest instead of WWW.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Check what you sending. Check what you are receiving.
    On PHP side, print out what you are getting.
     
  5. FVelasco

    FVelasco

    Joined:
    Jul 12, 2019
    Posts:
    12
    As I have said, when I print form.data it returns System.Byte[]. What do I print on PHP?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Variables requested by $_POST.
    Check your http://linxdatos.000webhostapp.com/Registrar.php if works correctly, without Unity.
    Investigate following.
    Code (CSharp):
    1. bool(false) string(107) "INSERT INTO `usuarios` ( `id` , `usuario` , `email` , `contraseña` ) VALUES ('' , '' , '' , '') ; " Error: Incorrect integer value: '' for column `id11649923_linxdb`.`usuarios`.`id` at row 1
    Also, make sure, you change DB, login and password now, since you exposed them to public.
     
  7. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Like Antypodish said.
    Also here
    Code (CSharp):
    1. $query = "INSERT INTO `usuarios` ( `id` , `usuario` , `email` , `contraseña` ) VALUES ('' , '".$usuario."' , '".$email."' , '".$contraseña."') ; ";
    id is INTEGER 100%. And when you're trying to insert into db you'll trying to add a string.
    use this
    Code (CSharp):
    1. $query = "INSERT INTO `usuarios` ( `id` , `usuario` , `email` , `contraseña` ) VALUES (null , '".$usuario."' , '".$email."' , '".$contraseña."') ; ";