So I have this application in Unity that I am making and I am trying to connect it to a mySQL database that I made and connected to a website. I am able to insert things through a form on the website into the database. That is working.
What I want to do next is connect the Unity app so that it can also access the things in the database. I am coding in C# and php. I want the unity application to ask the website for certain information from the database. The website should then look in the database and return some info to the unity application.
THIS IS NOT A DUPLICATE question. I have looked at the questions on here and I still can't get it to work. Right now my unity application is able to send a message to my webpage which my webpage then echos properly. (I know this isn't the functionality I talked about but I am just testing right now). However when I then go try to get my response in my Unity application from my webpage, all I debug is <html>
.
You can access my website at: http://historicstructures.org/forms.html
Here is my php code:
<html>
<style type="text/css">
body {background-color:#666666; color: white;}
</style>
<body>
<h1 align = "center">
<img src="housebackground.jpg" alt="Mountain View" style="width:97%;height:228px;" ></h1>
<h1 align = "center">Submission Status</h1>
<p align = "center">
<?php
//this is the variable that is being recieved from the unity script
$AuthorName = $_POST["Author"];
//here i am printing it out so that it will be sent back to the unity script
// i am also echoing it onto the webpage so that i know it is getting the variable
//correctly from the unity script
echo $AuthorName;
header("Access-Control-Allow-Origin: *");
print($AuthorName);
//gets all the variables the user inputted to form
$StructureName = $_POST["StructureName"];
$Author = $_POST["Author"];
$YearBuilt = $_POST["YearBuilt"];
$EraBuilt = $_POST["EraBuilt"];
$YearDestroyed = $_POST["YearDestroyed"];
$EraDestroyed = $_POST["EraDestroyed"];
$Latitude = $_POST["Latitude"];
$Longitude = $_POST["Longitude"];
$Structurelink = "no exist yet";
//checks to make sure the information is in the right format
$isValid = true;
$errCode = 0;
if ($Latitude<-90 || $Latitude>90){
$isValid = false;
$errCode = 1;
}
if ($Longitude<-180 || $Longitude>180){
$isValid = false;
$errCode = 2;
}
if ($YearBuilt<-400 || $YearBuilt>400){
$isValid = false;
$errCode = 3;
}
if ($YearDestroyed<-400 || $YearDestroyed>400){
$isValid = false;
$errCode = 4;
}
//if the informationt the user gave was correct, then insert into database
if ($isValid ==true){
$servername = "localhost";
$username = "...";
$password = "...";
$dbname = "StructureInfo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO info (ID, StructureName, Author, YearBuilt, EraBuilt, YearDestroyed, EraDestroyed, Latitude, Longitude, StructureLink)
VALUES ('null','$StructureName','$Author','$YearBuilt','$EraBuilt','$YearDestroyed','$EraDestroyed','$Latitude','$Longitude','$Structurelink')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
//the user has an error in the information they inputted
else{
echo "Your submission was invalid and so it was not submitted. ";
switch ($errCode) {
case 1:
echo "Your latitude is out of bounds.";
break;
case 2:
echo "Your longitude is out of bounds. ";
break;
case 3:
echo "Your year built is out of bounds. ";
break;
case 4:
echo "Your year destroyed is out of bounds. ";
break;
default:
echo "Go back and review your data to make sure it is correct.";
}
}
?>
</p>
<br><br>
</body>
</html>
Here is my unity code which is attached to a button, I wasn't sure where to put it so my onclick for the button is the main of this js:
#pragma strict
function Start () {
}
function Update () {
}
function GetFromDB(){
var url = "http://historicstructures.org/action_page_post.php";
var form = new WWWForm();
form.AddField( "Author", "Jess" );
var www = new WWW( url, form );
// wait for request to complete
yield www;
// and check for errors
if (www.error == null)
{
Debug.Log(www.text);
} else {
// something wrong!
Debug.Log("WWW Error: "+ www.error);
}
}
GetFromDB();