Stuck at yield return www.SendWebRequest();

Hello.

I’m trying to add my user to the DB. That works, but it’s not executing my else code after the yield return. When i print before and after the yield but it prints only “This is before the yield” and no “user added to team”. But in my DB it’s added so my PHP code works.

 IEnumerator UploadUserToDB()
    {

        WWWForm form = new WWWForm();
        form.AddField("name", NameField.text);
        form.AddField("uniqueID", playermanager.tempID);
        form.AddField("teamName", teamField.text);
  
        using (UnityWebRequest www = UnityWebRequest.Post("http Link", form))
        {

            print("before yield");
            yield return www.SendWebRequest();
            print("after yield");
            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                print("user is added to team");
            }
        }
    }

PHP CODE

function AddUserToTeam($laatsteUserID, $laatsteTeamID)
  {

    //DB INFO

      // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }

    $sql = "INSERT INTO `team_user` (`id`, `user_id`, `team_id`) VALUES (NULL, '$laatsteUserID', '$laatsteTeamID');";
    if ($conn->query($sql) === TRUE) {
      echo "User added to team";
    } else {
      echo "Error: " . $sql . "
" . $conn->error;
    }
  }

as I know, to send or get requests you need to use https link

There are only two reasons why it wouldn’t execute code after yield return:

  • An exception is thrown
  • The server accepts the request but does not send the response

You should get some error printed in console for the (1).
You can test the second by setting a timeout of UWR, just don’t make it too small, since then requests may simply fail because server was too slow to respond.

Another reasons why a coroutine does not continue:

  • ||

  • ||

  • The coroutine could have been stopped from the outside by StopCoroutine or StopAllCoroutines.

  • The gameobject / monobehaviour that is hosting the coroutine may have been destroyed or deactivated.

Regarding your PHP code: Currently your API is vulnerable to SQL injection. If you want to continue to use mysqli, please look up how to pass user content into sql statements in a secure manner. You may also want to switch to PDOs and prepared statements. With prepared statements you pass in your variables seperately and the PDO framework will make sure the passed arguments are escaped properly.

Also I guess your “id” column is an auto_increment column. You currently pass “null” into the id field. When you insert values, just don’t pass the id at all.

2 Likes

I have a routine who download multiple files, and i have something similar:

UnityWebRequest www;

www = UnityWebRequest.Get(url);
www.downloadHandler = new DownloadHandlerFile(savePath);


Debug.Log("1.1");
yield return www.SendWebRequest();
Debug.Log("1.2");

This is inside of a while loop, and my corutine just continue to the next loop so the point 3 an 4 of “Bunny83” is not because the corutine still a live, for the point 1 i not recibe any error below error because the corutine just jump to the next loop so it just retry the download instantly but work on this time in less than a second making exactly the same petition i dont know if is the point 2 because the time is automatic and work the second time.

I think that it is a silent bug, the request just end without error and the process not end the execution because on the second loop iteration when i try to download the same file the console give me this error:

ArgumentException: Failed to create file C:/Users/olive/AppData/LocalLow/BOT/Adventured\01\map_config.json
UnityEngine.Networking.DownloadHandlerFile.InternalCreateVFS (System.String path, System.Boolean append) (at <1abe9eafdcf148ab9a14dd434f1379a0>:0)
UnityEngine.Networking.DownloadHandlerFile…ctor (System.String path) (at <1abe9eafdcf148ab9a14dd434f1379a0>:0)
Adventured.Web.scr_web_download_files+d__3.MoveNext () (at Assets/_scripts/Web/scr_web_download_files.cs:88)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <88d854ea2c91426ebc464f01cd71aa85>:0)