unity3d form.Addfield不会将变量发送到PHP文件

时间:2017-12-01 07:04:21

标签: c# php forms unity3d

我真的陷入了一些我不知道为什么不能正常工作的事情。 我的目标是将一个POST变量从unity发送到php文件。 php文件工作正常,当我提交一个带有常规HTML表单的POST时,它正在工作。但是当我从统一发送它时,然后php说Notice: Undefined index: 这是代码(我使用的是Unity本身的一个例子)

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

public class FormSender : MonoBehaviour
{

public string discipline;

void Start()
{
    StartCoroutine(Upload());
}

IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("Discipline", discipline);

    UnityWebRequest www = UnityWebRequest.Post("linktomyworkingphpfile.php", form);
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }
}
}

在我的php文件中,我检索它:

$discipline = $_POST['Discipline'];

(我不认为它是在PHP中,因为使用常规HTML表单它工作正常)。 我真的不明白我做错了什么。 有人能帮我吗?? 日Thnx

这是统一资源的例子:

using UnityEngine;
using System.Collections;

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

    IEnumerator Upload() {
        WWWForm form = new WWWForm();
        form.AddField("myField", "myData");

        UnityWebRequest www = UnityWebRequest.Post("linktomyworkingphpfile.php", form);
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error);
        }
        else {
            Debug.Log("Form upload complete!");
        }
    }
}

我看到的唯一区别是团结的例子有公共单一行为吗?但是当我这样做时它给了我一个编译错误:意外的符号public', expecting类型'

在我的控制台中,它还显示调试"表格上传完成!"。

我真的很困惑 PS。我发送的用于纪律的字符串是100%正确的(复制粘贴,以html形式工作而不是通过统一)

更新代码:

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

public class FormSender : MonoBehaviour
{

public string discipline;

  void Start()
    {
        StartCoroutine(Upload());
        StartCoroutine(GetText());
    }






    IEnumerator Upload()
{
    WWWForm form = new WWWForm();
    form.AddField("Discipline", discipline);

    UnityWebRequest www = UnityWebRequest.Post("http://localhost/alchemywebinterface/unity/categories.php", form);
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }
}

    IEnumerator GetText()
    {
        UnityWebRequest www = UnityWebRequest.Get("http://localhost/alchemywebinterface/unity/categories.php");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            // Show results as text
            Debug.Log("TEXT = " + www.downloadHandler.text);

            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
        }
    }
}

输出:

 TEXT = <br />
<b>Notice</b>:  Undefined index: Discipline in <b>C:\xampp\htdocs\AlchemyWebInterface\unity\categories.php</b> on line <b>11</b><br />
test textProduct categorie: Product Cat 1;Product categorie: Product Cat 2;Product categorie: Visualisatie;Product categorie: Simulatie;    

Form upload complete!

PHP文件:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbName = "test";

    //make connection

    $conn = new mysqli ($servername, $username, $password, $dbName);

    $discipline = $_POST['Discipline'];
   // $discipline = "Virtual Experience";

    //Check Connection
    if(!$conn){
        die("Connection Failed. ". mysqli_connect_error());
    }

echo ("test text");


    //$sqlCat = "SELECT distinct product_cat FROM product where discipline_naam='".$discipline."'"; 
    $sqlCat = "SELECT distinct product_cat FROM product";
    $resultCat = mysqli_query($conn ,$sqlCat);  

    if(mysqli_num_rows($resultCat) > 0){        //show data for each row
        while($row = mysqli_fetch_assoc($resultCat)){
            echo "Product categorie: ".$row['product_cat'] . ";"; 

        }
    }


?>  

0 个答案:

没有答案