Search Unity

I can retrieve data from my web host, but I cannot post data to my database.

Discussion in 'Scripting' started by Bucuru, Sep 13, 2019.

  1. Bucuru

    Bucuru

    Joined:
    Sep 13, 2019
    Posts:
    4
    This is one of my first times posting here so sorry if I'm not wording everything perfectly. I looked around and haven't been able to find a good answer for my issue here and hopefully this will net some good advice.



    I followed the tutorial here and everything was working great until I got to this last video. I'm pretty sure the issue lies somewhere within my code and not the set up of the database using this 000webhost website, but maybe the problem is there somewhere. I think it's my code because I can load data from the website into my project in Unity just fine. If I add the data with myphpAdmin, it will show up in game. But when I try to send that same data from inside Unity I get nothing, not even errors in the console.

    These first two scripts are my php files, I've deleted the password for security purposes but otherwise I believe that part is fine.

    InsertUser.php

    Code (CSharp):
    1. <?php
    2.     $server_Name = "localhost";
    3.     $server_userName = "id10860574_brknbladebucuru";
    4.     $server_passWord = "";
    5.     $dbName = "id10860574_abstractiadb";
    6.    
    7.     $name = $_POST["namePost"];
    8.     $class = $_POST["classPost"];
    9.     $score = $_POST["scorePost"];
    10.     $timeScore = $_POST["timeScorePost"];
    11.    
    12.     //Make Connection
    13.    
    14.     $conn =  new mysqli($server_Name, $server_userName, $server_passWord, $dbName);
    15.    
    16.     //Check Connection
    17.    
    18.     if(!$conn)
    19.     {
    20.         die("Connection Failed". mysqli_connect_error());
    21.     }
    22.    
    23.     $sql = "INSERT INTO scores(name, class, score, time)
    24.            VALUES ('".$name."', '".$class."', '".$score."', '".$timeScore."')";
    25.     $result = mysqli_query($conn ,$sql);
    26.    
    27.     if(!result) echo "there was an error";
    28.     else echo "Everything ok.";
    29.  
    30. ?>
    DataLoader.php

    Code (CSharp):
    1. <?php
    2.     $serverName = "localhost";
    3.     $userName = "id10860574_brknbladebucuru";
    4.     $passWord = "";
    5.     $dbName = "id10860574_abstractiadb";
    6.    
    7.     //Make Connection
    8.    
    9.     $conn =  new mysqli($serverName, $userName, $passWord, $dbName);
    10.    
    11.     //Check Connection
    12.    
    13.     if(!$conn)
    14.     {
    15.         die("Connection Failed". mysqli_connect_error());
    16.     }
    17.    
    18.     $sql = "SELECT ID, Class, Time, Score, Name FROM scores ORDER BY Score DESC";
    19.     $result = mysqli_query($conn ,$sql);
    20.    
    21.    
    22.     if(mysqli_num_rows($result) > 0){
    23.         //show data for each row
    24.         while($row = mysqli_fetch_assoc($result)){
    25.             echo "ID:".$row['ID'] . "|Class:".$row['Class']. "|Time:".$row['Time']. "|Score:".$row['Score']. "|Name:".$row['Name'] . ";";
    26.         }
    27.        
    28.     }
    29. ?>
    Here is the c#

    DataLoader.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class DataLoader : MonoBehaviour
    8. {
    9.     public string[] scores;
    10.  
    11.     public Text scoreTextOne;
    12.     public Text scoreTextTwo;
    13.     public Text scoreTextThree;
    14.     public Text scoreTextFour;
    15.     public Text scoreTextFive;
    16.     public Text scoreTextSix;
    17.     public Text scoreTextSeven;
    18.     public Text scoreTextEight;
    19.     public Text scoreTextNine;
    20.     public Text scoreTextTen;
    21.  
    22.     int counter;
    23.  
    24.     // Start is called before the first frame update
    25.     IEnumerator Start()
    26.     {
    27.         WWW scoresData = new WWW("http://brokenbladestudios.000webhostapp.com/ScoresData.php");
    28.         yield return scoresData;
    29.         string scoresDataString = scoresData.text;
    30.         scores = scoresDataString.Split(';');
    31.         FillScoreTextBoxes();
    32.     }
    33.  
    34.     void FillScoreTextBoxes()
    35.     {
    36.         FillScoreTextBoxOne();
    37.         FillScoreTextBoxTwo();
    38.         FillScoreTextBoxThree();
    39.         FillScoreTextBoxFour();
    40.         FillScoreTextBoxFive();
    41.         FillScoreTextBoxSix();
    42.         FillScoreTextBoxSeven();
    43.         FillScoreTextBoxEight();
    44.         FillScoreTextBoxNine();
    45.         FillScoreTextBoxTen();
    46.     }
    47.  
    48.     void FillScoreTextBoxOne()
    49.     {
    50.         if (scores.Length - 1 >= 1)
    51.         {
    52.             if (scores[0] != null)
    53.             {
    54.                 scoreTextOne.text = string.Format("1    {0}      {1}       {2}      {3}",
    55.                 GetDataValue(scores[0], "Name:"), ChangeClassFormat(0), GetDataValue(scores[0], "Score:"), ChangeTimeFormat(0));
    56.             }
    57.         }
    58.     }
    59.     void FillScoreTextBoxTwo()
    60.     {
    61.         if (scores.Length - 1 >= 2)
    62.         {
    63.             if (scores[1] != null)
    64.             {
    65.                 scoreTextTwo.text = string.Format("2    {0}      {1}       {2}      {3}",
    66.                 GetDataValue(scores[1], "Name:"), ChangeClassFormat(1), GetDataValue(scores[1], "Score:"), ChangeTimeFormat(1));
    67.             }
    68.         }
    69.     }
    70.     void FillScoreTextBoxThree()
    71.     {
    72.         if (scores.Length - 1 >= 3)
    73.         {
    74.             if (scores[2] != null)
    75.             {
    76.                 scoreTextThree.text = string.Format("3    {0}      {1}       {2}      {3}",
    77.                 GetDataValue(scores[2], "Name:"), ChangeClassFormat(2), GetDataValue(scores[2], "Score:"), ChangeTimeFormat(2));
    78.             }
    79.         }
    80.     }
    81.     void FillScoreTextBoxFour()
    82.     {
    83.         if (scores.Length - 1 >= 4)
    84.         {
    85.             if (scores[3] != null)
    86.             {
    87.                 scoreTextFour.text = string.Format("4    {0}      {1}       {2}      {3}",
    88.                 GetDataValue(scores[3], "Name:"), ChangeClassFormat(3), GetDataValue(scores[3], "Score:"), ChangeTimeFormat(3));
    89.             }
    90.         }
    91.     }
    92.     void FillScoreTextBoxFive()
    93.     {
    94.         if (scores.Length - 1 >= 5)
    95.         {
    96.             if (scores[4] != null)
    97.             {
    98.                 scoreTextFive.text = string.Format("5    {0}      {1}       {2}      {3}",
    99.                 GetDataValue(scores[4], "Name:"), ChangeClassFormat(4), GetDataValue(scores[4], "Score:"), ChangeTimeFormat(4));
    100.             }
    101.         }
    102.     }
    103.     void FillScoreTextBoxSix()
    104.     {
    105.         if (scores.Length - 1 >= 6)
    106.         {
    107.             if (scores[5] != null)
    108.             {
    109.                 scoreTextSix.text = string.Format("6    {0}      {1}       {2}      {3}",
    110.                 GetDataValue(scores[5], "Name:"), ChangeClassFormat(5), GetDataValue(scores[5], "Score:"), ChangeTimeFormat(5));
    111.             }
    112.         }
    113.     }
    114.     void FillScoreTextBoxSeven()
    115.     {
    116.         if (scores.Length - 1 >= 7)
    117.         {
    118.             if (scores[6] != null)
    119.             {
    120.                 scoreTextSeven.text = string.Format("7    {0}      {1}       {2}      {3}",
    121.                 GetDataValue(scores[6], "Name:"), ChangeClassFormat(6), GetDataValue(scores[6], "Score:"), ChangeTimeFormat(6));
    122.             }
    123.         }
    124.     }
    125.     void FillScoreTextBoxEight()
    126.     {
    127.         if (scores.Length - 1 >= 8)
    128.         {
    129.             if (scores[7] != null)
    130.             {
    131.                 scoreTextEight.text = string.Format("8    {0}      {1}       {2}      {3}",
    132.                 GetDataValue(scores[7], "Name:"), ChangeClassFormat(7), GetDataValue(scores[7], "Score:"), ChangeTimeFormat(7));
    133.             }
    134.         }
    135.     }
    136.     void FillScoreTextBoxNine()
    137.     {
    138.         if (scores.Length - 1 >= 9)
    139.         {
    140.             if (scores[8] != null)
    141.             {
    142.                 scoreTextNine.text = string.Format("9    {0}      {1}       {2}      {3}",
    143.                 GetDataValue(scores[8], "Name:"), ChangeClassFormat(8), GetDataValue(scores[8], "Score:"), ChangeTimeFormat(8));
    144.             }
    145.         }
    146.     }
    147.     void FillScoreTextBoxTen()
    148.     {
    149.         if (scores.Length - 1 >= 10)
    150.         {
    151.             if (scores[9] != null)
    152.             {
    153.                 scoreTextTen.text = string.Format("10    {0}      {1}       {2}      {3}",
    154.                 GetDataValue(scores[9], "Name:"), ChangeClassFormat(9), GetDataValue(scores[9], "Score:"), ChangeTimeFormat(9));
    155.             }
    156.         }
    157.     }
    158.  
    159.     string GetDataValue(string data, string index)
    160.     {
    161.         string value = data.Substring(data.IndexOf(index) + index.Length);
    162.         if (value.Contains("|"))
    163.         {
    164.             value = value.Remove(value.IndexOf("|"));
    165.         }
    166.  
    167.         return value;
    168.     }
    169.  
    170.     string ChangeTimeFormat(int index)
    171.     {
    172.         int timeNumber;
    173.         int secondsNumber = 0;
    174.         int minutesNumber = 0;
    175.         string secondsString;
    176.         string minutesString;
    177.         timeNumber = int.Parse(GetDataValue(scores[index], "Time:"));
    178.  
    179.         for (int t = timeNumber; t >= 60; t -= 60)
    180.         {
    181.             timeNumber -= 60;
    182.             minutesNumber++;
    183.         }
    184.         secondsNumber = timeNumber;
    185.  
    186.         if (secondsNumber <= 9)
    187.         {
    188.             secondsString = string.Format("0{0}", secondsNumber);
    189.         }
    190.         else
    191.         {
    192.             secondsString = secondsNumber.ToString();
    193.         }
    194.         if (minutesNumber <= 9)
    195.         {
    196.             minutesString = string.Format("0{0}", minutesNumber);
    197.         }
    198.         else
    199.         {
    200.             minutesString = minutesNumber.ToString();
    201.         }
    202.  
    203.         return string.Format("{0} : {1}", minutesString, secondsString);
    204.  
    205.  
    206.  
    207.         //return "";
    208.     }
    209.    
    210.     string ChangeClassFormat(int index)
    211.     {
    212.         int classNumber;
    213.  
    214.         classNumber = int.Parse(GetDataValue(scores[index], "Class:"));
    215.  
    216.         if (classNumber == 1)
    217.         {
    218.             return "Fighter";
    219.         }
    220.         else if (classNumber == 2)
    221.         {
    222.             return "Caster";
    223.         }
    224.         else if (classNumber == 3)
    225.         {
    226.             return "Hunter";
    227.         }
    228.         else
    229.         {
    230.             return "";
    231.         }
    232.     }
    233. }
    DataInserter.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DataInserter : MonoBehaviour
    7. {
    8.     public string nameName;
    9.     public string className;
    10.     public string score;
    11.     public string timeScore;
    12.  
    13.     string CreateUserURL = "http://brokenbladestudios.000webhostapp.com/InsertUser.php";
    14.  
    15.     public void CreateUser(string usernameValue, string classNameValue, string scoreValue, string timeScoreValue)
    16.     {
    17.         WWWForm form = new WWWForm();
    18.         form.AddField("namePost", usernameValue);
    19.         form.AddField("classPost", classNameValue);
    20.         form.AddField("scorePost", scoreValue);
    21.         form.AddField("timeScorePost", timeScoreValue);
    22.  
    23.         WWW www = new WWW(CreateUserURL, form);
    24.     }
    25. }
    I apologize in advance for not writing the DataLoader more efficiently, like I said, that part works, so don't bother reading all of it if you don't want to.

    I'm kinda hoping this is something silly that I missed but I feel like it's more along the lines of I'm super new to SQL and PHP and there's something I just don't understand fundamentally going on. Thanks in advance for any helpful advice you have! I understand that this isn't secure and I'll study up on that and fix it in the future, but for now I'm just trying to get this to work. The only people that will access it are close friends of mine and I just want a testable version of my project that they can all download.
     
  2. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    216
    In CreateUser, use a coroutine, and actually yield return your www!
     
  3. Bucuru

    Bucuru

    Joined:
    Sep 13, 2019
    Posts:
    4
    Now my DataInster.cs looks like this, I'm calling StartCoroutine(dataInserter.CreateUser()) from a separate InputScript.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DataInserter : MonoBehaviour
    7. {
    8.     public string nameName;
    9.     public string className;
    10.     public string score;
    11.     public string timeScore;
    12.  
    13.     string CreateUserURL = "http://brokenbladestudios.000webhostapp.com/InsertUser.php";
    14.  
    15.     public IEnumerator CreateUser(string usernameValue, string classNameValue, string scoreValue, string timeScoreValue)
    16.     {
    17.         WWWForm form = new WWWForm();
    18.         form.AddField("namePost", usernameValue);
    19.         form.AddField("classPost", classNameValue);
    20.         form.AddField("scorePost", scoreValue);
    21.         form.AddField("timeScorePost", timeScoreValue);
    22.  
    23.         WWW www = new WWW(CreateUserURL, form);
    24.  
    25.         yield return www;
    26.         SceneManager.LoadScene(0);
    27.     }
    28. }
    Still no luck though, my scene does get reloaded so we make it all the way through, yet I can still only load my data, the new data does not post.
     
  4. Bucuru

    Bucuru

    Joined:
    Sep 13, 2019
    Posts:
    4
    Bump^
     
  5. Bucuru

    Bucuru

    Joined:
    Sep 13, 2019
    Posts:
    4
    I keep seeing posts like mine with slight variations, none of them have had a fix on them though. Is this method of using web forms to send data obsolete in unity now? It's okay if I need to learn some other way of accomplishing it. But I'd really like to know if I'm missing something small so I don't have to waste all the work I did so far.