Hi all, sorry to bother you guys, but is 3 days now that I’m banging effortlessly my head on this wall.
Already searched the searcheble with the key words “problem post php unity” and combination, on google unity forum and stack overflow. Still no answer.
I’m setting up an user registration system that connect to an online database (mysql). To do so, I have a couple of php file that sends the query to the server. Here is the php script:
<?php
//Variables for the connection
$servername = "server_name";
$server_username = "server_username";
$server_password = "server_password";
$dbName = "db_name";
//Variable from the user
$username = $_POST['usernamePost'];
$email = $_POST['emailPost'];
$password = $_POST['passwordPost'];
$experience = $_POST['experiencePost'];
$prova = "ciaone";
echo "username is: $username";
echo "email is: $email";
echo "password is: $password";
echo "experience is: $experience";
//Make Connection
$conn = new mysqli($servername, $server_username, $server_password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
$sql = "INSERT INTO users (username, email, password, experience)
VALUES ('".$username."','".$prova."','".$password."','".$experience."')";
$result = mysqli_query($conn ,$sql);
if(!result) echo "there was an error";
else echo "Everything ok.";
?>
Now, I’ve tried with WWW object and with UnityWebRequest object. I create a WWWForm object, add some fields and send the request. Unity does the trick, and insert a new instance on my database. The problem is that I’m not able to fill in the POST variables.
Here is the script I’ve used the UnityWebRequest:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public static class RegisterUser {
static string register_new_user_php = "server_url/RegisterNewUser.php";
public static IEnumerator RegisterNewUser(string new_username, string new_email_address, string new_password)
{
WWWForm form = new WWWForm();
form.AddField("usernamePost", "TEST");
form.AddField("emailPost", "TEST");
form.AddField("passwordPost", "TEST");
UnityWebRequest www = UnityWebRequest.Post(register_new_user_php, form);
//www.chunkedTransfer = false; possible solution found somewhere, not working
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log("RegisterUser:: " + www.error);
}
else
{
Debug.Log("RegisterUser::Form upload complete!");
}
}
}
And here I used the WWW:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public static class RegisterUser {
static string register_new_user_php = "server_url/RegisterNewUser.php";
public static IEnumerator RegisterNewUser(string new_username, string new_email_address, string new_password)
{
WWWForm form = new WWWForm();
form.AddField("usernamePost", "TEST");
form.AddField("emailPost", "TEST");
form.AddField("passwordPost", "TEST");
WWW www = new WWW(register_new_user_php, form);
//www.chunkedTransfer = false;
yield return www;
if (www.error != null)
{
Debug.Log("RegisterUser:: " + www.error);
}
else
{
Debug.Log("RegisterUser::Form upload complete!");
Debug.Log("RegisterUser::MessageFromWWW: " + www.text);
}
}
}
Now, the php part seem to work: stuff got added on the db, but their fields remains empty.
Also, when executing the WWW script, I was able to display on Debug the result of the page request, and it display: “username is: email is: password is:” (as the result of the last line of the script, the debug(www.text)).
So I really think the problem is that unity somehow isn’t able to tell the php script the field’s values.
If anyone knows how to work this around that would be great.
If not, it will also be good to hear other way to make unity talk with online database, that would be great too.
Thank you all!