【发布时间】:2017-09-08 23:48:51
【问题描述】:
所以我在 Unity 中有这个应用程序,我正在尝试将它连接到我制作并连接到网站的 mySQL 数据库。我可以通过网站上的表格将内容插入数据库。那是有效的。
接下来我要做的是连接 Unity 应用程序,以便它也可以访问数据库中的内容。我正在用 C# 和 php 编写代码。我希望统一应用程序向网站询问数据库中的某些信息。然后网站应该查看数据库并将一些信息返回给统一应用程序。
这不是一个重复的问题。我已经查看了这里的问题,但仍然无法正常工作。现在我的统一应用程序能够向我的网页发送一条消息,然后我的网页会正确回显。 (我知道这不是我谈到的功能,但我现在只是在测试)。但是,当我尝试从我的网页中获取我的 Unity 应用程序中的响应时,我调试的只是<html>。
您可以访问我的网站:http://historicstructures.org/forms.html
这是我的 php 代码:
<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>
这是我的统一代码,它附加到一个按钮,我不知道把它放在哪里,所以我对按钮的 onclick 是这个 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();
【问题讨论】:
标签: c# php mysql database unity3d