I use Unity (C#) to connect to my server code (written in PHP) and send user input to PHP's $_POST
part.
I tested that server side works well, it can send value to my database (to user
table). Unity part also works well by itself, but once I try to connect them together, it only sends the empty value to my user table.
Also, the auto-increased ID of the user
table let me know that C# has properly connected to the php server side.
Why this is not working, how can I resolve it?
Edit: I tried to break line test, and both of c# and php works ok by itself. Once I use C# to send data to php, the data values (username password email)
are empty.
Data table:
Problem:(No data send to PHP by C#):
C#:
using UnityEngine;
using System.Collections;
public class DataInserter : MonoBehaviour
{
public string inputUserName;
public string inputPassword;
public string inputEmail;
string CreateUserURL = "http://localhost/prevision/insertUser.php";
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
CreateUser(inputUserName, inputPassword, inputEmail);
}
public void CreateUser(string username, string password, string email)
{
WWWForm form = new WWWForm();
form.AddField("UserNamePost", username);
form.AddField("PasswordPost", password);
form.AddField("EmailPost", email);
WWW www = new WWW(CreateUserURL, form);
}
}
PHP:
<?php
include("dbConnection.php");
//check connection
if(!$connection)
{
die("no connection". mysqli_connect_error());
}
else
echo "<h1>Connected</h1>"."<br>";
$UserName = $_POST["UserNamePost"];
$Password = $_POST["PasswordPost"];
$Email = $_POST["EmailPost"];
$sql = "INSERT INTO User (UserName,Password,Email)
VALUES ('".$UserName."','".$Password."','".$Email."')";
$result = mysqli_query($connection, $sql);
if(!result)
{
echo"Wrong";
}
else
echo "Good";
?>
I have had similar problems in Unity with sending a Post request.
It all worked fine, but somewhere or somehow Unity/PHP stopped working correctly because of incorrect header types. I switched classes for connecting to the network and setting chunckedtransfer to false. Here is what works for me
public IEnumerator CreateUser(string username, string password, string email)
{
WWWForm form = new WWWForm();
form.AddField("UserNamePost", username);
form.AddField("PasswordPost", password);
form.AddField("EmailPost", email);
UnityWebRequest www = UnityWebRequest.Post(CreateUserURL, form);
www.chunkedTransfer = false;
yield return www.SendWebRequest();
if(www.error == null){
// you can place code here for handle a succesful post
} else {
// what to do on error
}
}
You can then call your function like a Coroutine
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
StartCoroutine(CreateUser(inputUserName, inputPassword, inputEmail));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.