php $_POST is blank when I send variables from unity
Hello guys, I'm having a really annoying problem with php. With c# I send variables to php, and I want php to save it into my database. But the $_POST in php is empty.
In Unity I did a Debug.Log of the variables I am sending to php right before it sends to see if it works, and the variables are there. BUT in php when I echo the $_POST, it returns nothing, it's empty and in the database it adds emptiness.
C#:
public IEnumerator replaceData(string toggle1Post, string toggle2Post,
string toggle3Post, string toggle4Post,
string toggle5Post, string toggle6Post)
{
WWWForm form = new WWWForm();
form.AddField("toggle1Post", toggle1Post);
form.AddField("toggle2Post", toggle2Post);
form.AddField("toggle3Post", toggle3Post);
form.AddField("toggle4Post", toggle4Post);
form.AddField("toggle5Post", toggle5Post);
form.AddField("toggle6Post", toggle6Post);
WWW www = new WWW(parkingURL, form);
yield return www;
Debug.Log(www.text);
}
php:
$parkingName1 = "Bus Gate";
$toggle1Post = $_POST['toggle1Post'];
$parkingName2 = "Pepsi Gate";
$toggle2Post = $_POST['toggle2Post'];
$parkingName3 = "Watson";
$toggle3Post = $_POST['toggle3Post'];
$parkingName4 = "PVA";
$toggle4Post = $_POST['toggle4Post'];
$parkingName5 = "Gardens";
$toggle5Post = $_POST['toggle5Post'];
$parkingName6 = "Omar Mohsen";
$toggle6Post = $_POST['toggle6Post'];
//Make the connection_aborted
$conn = new mysqli($server_name, $server_username, $server_password, $dbName);
//Check connection_aborted
if (!$conn)
{
die("Connection Failed. ". mysqli_connect_error());
}
else
{
//echo("Connection Success" . "<br>");
}
$sql1 = "UPDATE parking SET Toggle = '$toggle1Post' WHERE gateName = '$parkingName1'";
if (mysqli_query($conn, $sql1)) echo " Updated " . $toggle1Post;
else echo "notUpdated: " . mysqli_error($conn);
$sql2 = "UPDATE parking SET Toggle = '$toggle2Post' WHERE gateName = '$parkingName2'";
if (mysqli_query($conn, $sql2)) echo " Updated " . $toggle2Post;
else echo "notUpdated: " . mysqli_error($conn);
$sql3 = "UPDATE parking SET Toggle = '$toggle3Post' WHERE gateName = '$parkingName3'";
if (mysqli_query($conn, $sql3)) echo " Updated " . $toggle3Post;
else echo "notUpdated: " . mysqli_error($conn);
$sql4 = "UPDATE parking SET Toggle = '$toggle4Post' WHERE gateName = '$parkingName4'";
if (mysqli_query($conn, $sql4)) echo " Updated " . $toggle4Post;
else echo "notUpdated: " . mysqli_error($conn);
$sql5 = "UPDATE parking SET Toggle = '$toggle5Post' WHERE gateName = '$parkingName5'";
if (mysqli_query($conn, $sql5)) echo " Updated " . $toggle5Post;
else echo "notUpdated: " . mysqli_error($conn);
$sql6 = "UPDATE parking SET Toggle = '$toggle6Post' WHERE gateName = '$parkingName6'";
if (mysqli_query($conn, $sql6)) echo " Updated " . $toggle6Post;
else echo "notUpdated: " . mysqli_error($conn);
Update 2: I tried the same code on another computer with unity version 5.4.1f1 and it worked there, on my computer I'm on version 2017.3.0f3 and it does not work.
Update 3: It works on the latest update :)
Answer by YongPing92 · Apr 09, 2018 at 03:55 AM
In-case someone is getting empty $POST variable using php, I solved it just by changing the link of the php file from using http://example.com/projects/list.php to https://example.com/projects/list.php (difference is "http" and "https".)
Hope this helps someone
You're my lifesaver! I just wasted 3-4 hours with searching and debugging, changing everything what's possible...i was close to insanity. And the answer is a simple "s" :D Thanks a lot!
Answer by awtrimpe · Feb 15, 2018 at 03:16 PM
Thanks for the suggestion! I have been looking for a fix for three days so I will try a different version of Unity!
Answer by Sudarmin-Then · Mar 12, 2018 at 05:07 PM
@2000noe Damn, I've been trying to make it work for like a day, I was using version 2017.3.0f3, now downloading the latest version, hopefully it would work. Thanks for sharing, otherwise I would have spent another day on this
Your answer
Follow this Question
Related Questions
Can I add a table to a mySQL database using C#? 0 Answers
C# safe connect to MySQL database. Download and upload data via PHP script. 1 Answer
500 Internal server error in WWWForm 0 Answers
Why is MySQL search query not returning each row in 1 index of an array? 0 Answers
WWW Class silent crach 1 Answer