My php page is not getting POST data from my C# script at all
P粉921165181
P粉921165181 2024-03-22 10:55:36
0
1
476

I have no problem creating valid SQL and PHP code. I have no problem creating C# code that runs internally.

My only problem is that I can't get my C# script (from within Unity, btw) to pass a string to ascended.us/monstro/login

That's it! Is there any smart person here who can help me overcome this annoying obstacle?

Thank you so much!

I'm trying to get the following code to send a form to my page. Yes, Unityis sending the network request, but it's not transmitting the post data. Here's the C# side of things.

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class MyBehavior : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Upload());
    }

    IEnumerator Upload()
    {
        using (UnityWebRequest www = UnityWebRequest.Post("https://www.ascended.us/monstro/login.php", "{ \"loginUser\": 1, \"field2\": 2 }", "application/json"))
        {
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
            }
        }
    }
}

Below is my PHP script for accepting C# UnityWebRequest

<?
        $sqllink = mysqli_connect("localhost", "REDACTED", "REDACTED", "REDACTED"); //THESE ARE VERIFIED TO BE CORRECT
    if (!$sqllink)
    {die('Could not connect: ' . mysqli_connect_errno() . ' - ' . mysqli_connect_error());}
        
        $loginUser = $_POST["loginUser"];
        
        $query = "INSERT INTO inbound_data (data) VALUES('$loginUser')";
        mysqli_query($sqllink, $query);
?>

I know this script is not secure, so treat it as a hello world application. When I run the script, what ends up happening is that it inserts a value into my SQL table, but the value is empty, indicating that the post variable was not transferred. can you help?

Maybe I need some kind of permission? I'm using BlueHost if that helps. MustThere is an easy way to actually get this post data sent from C# to my PHP page! I've tried other tutorials and similar responses on this site (and many others) but they don't work.

P粉921165181
P粉921165181

reply all(1)
P粉864872812

The

C# script looks fine for sending a POST request, but it seems like the data the PHP script is receiving may be in the wrong format. In your UnityWebRequest, you are sending JSON data, but your PHP script is trying to access $_POST["loginUser"], which is for regular form data, not JSON data.

You can modify the C# script to send the data as form data like this:

IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("loginUser", "1");
    form.AddField("field2", "2");

    using (UnityWebRequest www = UnityWebRequest.Post("https://www.ascended.us/monstro/login.php", form))
    {
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}

In a PHP script, you can access the data using $_POST:


With the modified code, you can send POST data from Unity as form data, and your PHP scripts can correctly access the POST variables using $_POST["loginUser"].

If you still have problems, please let me know.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template