Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GET request works but POST doesn't.

Discussion in 'Scripting' started by SamohtVII, Apr 27, 2020.

  1. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    368
    I have a script and php file that look like the below. I cannot get the POST data passed into the php file. Whenever I try to print it it says "Undefined index username" which obviosuly means the username POST data was not received. Oddly enough I can change this code to be GET instead of POST and it works. Why does GET work and not POST? I am using Unity 2019.3.5f1. It was working 2 weeks ago and I didn't change any code. I also, using version control, checked out a copy 2 weeks ago and it still doesn't work. Did Unity change anything? Are there settings in my web cPanel that may cause this? Are the headers not set correctly or something?

    Thanks

    Code (CSharp):
    1. public IEnumerator saveUsername() {
    2.         WWWForm form = new WWWForm();
    3.         form.AddField("username", scriptUsername);
    4.         WWW www = new WWW("https://www.mywebsite.com/DriftWorlds/SavePlayer.php", form);
    5.         yield return www;
    6.     }
    And the php page looks like this...

    Code (CSharp):
    1.     error_reporting(E_ALL);
    2.     ini_set('display_errors', 1);
    3.     error_reporting(2047);
    4.  
    5.     //database host (usually localhost)
    6.     define('DB_HOST', 'localhost');
    7.  
    8.     //database username
    9.     define('DB_USER', '//username//');
    10.  
    11.     //database password
    12.     define('DB_PASS', '//password//');
    13.  
    14.     //database schema name
    15.     define('DB_NAME', '//dbname//');
    16.  
    17.     require_once('mysqli_db.php');
    18.     $mysqli_db = new mysqli_db($port = null); //can also specify port if needed
    19.  
    20.  
    21.     $name = $_POST["username"];
    22.  
    23.     $insert_data = array(
    24.         'Username' => $name,
    25.         'Created' => date("Y-m-d")
    26.     );
    27.  
    28.         $insert_result = $mysqli_db->insert('Users', $insert_data);
    29.         if($insert_result){
    30.             $print .= '<p>Insert query succeeded.</p>';
    31.         }else{
    32.             $print .= '<p>Insert query failed.</p>';
    33.         }