Hello everyone, I am trying to insert values from Unity to a PHP then to mySQL. I’ve tried inserting manually in a browser’s address bar and it works fine, but when i use the WWW Form AddField, it connects successfully but inserts nothing, just blank values. I dont really know what’s wrong.
public class addInfo : MonoBehaviour
{
private string addInfoURL = "http://myurl.net/addInfo.php";
private string username1 = "Albert";
private string info1 = "this is my info";
void Update ()
{
if(Input.GetKeyDown("h"))
{
WWWForm form = new WWWForm();
form.AddField("username",username1);
form.AddField("info",info1);
WWW www = new WWW(addInfoURL, form);
StartCoroutine(addInfoToDB(www));
}
}
public IEnumerator addInfoToDB(WWW www)
{
yield return www;
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);
}
else
{
Debug.Log("WWW Error: "+ www.error);
}
}
}
Here’s my php:
<?php
$db = mysql_connect('host', 'username', 'pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('myDB') or die('Could not select database');
$username = $_GET['username'];
$info = $_GET['info'];
$query = "insert into myDB values (NULL, '$username', '$info')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>