我对 PHP 和 SQL 还很陌生,我正在尝试学习如何在 Unity 中使用它们。
首先,我使用 Unity 在数据库中创建了一个新表,运行正常。现在我想用一些信息更新这个表,这些信息应该来自Unity。 但这不起作用;我不确定这是我的 C# 脚本还是 PHP 脚本。 首先,这是我的 C# 脚本的片段:
IEnumerator InsertMemberData(string username,string day,int sale)
{
//insert data to table
WWWForm form = new WWWForm();
form.AddField("usernamePost", username);
form.AddField("currentDayPost", day);
form.AddField("daySalePost", sale);
UnityWebRequest www = UnityWebRequest.Post(updateDataTable, form);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
现在是我的 PHP 脚本
<?php
$servername = **********
$server_username = ***************
$server_password = ****************
$dbName = *****************
$username = $_POST["usernamePost"];
$currentDay = $_POST["currentDayPost"];
$daySale = $_Post["daySalePost"];
//Make Connection
$conn = new mysqli($servername, $server_username, $server_password, $dbName);
//Check Connection
if(!$conn)
{
die("Connection Failed". mysqli_connect_error());
}
$sql = "Update tbl_{$username} SET ".$currentDay." = ".$daySale." WHERE id=1";
$result = mysqli_query($conn, $sql);
if(!$result){
mysqli_error($conn);
echo "there was an Error!";
}
else echo "Evereything ok.";
?>
我只想在特定日期向特定表格发送一个数字。
最佳答案
我重新思考了我的工作。因此,没有针对每个用户的表,仅在包含所有用户/用户信息的表上,并且认为想要保存/加载。
C# 脚本或多或少相同:
IEnumerator InsertMemberData(string username,string day,int sale)
至
IEnumerator InsertMemberData(string username,string day,**string** sale)
和我的 PHP 脚本:
if($conn)
{
$sqlCheck = "SELECT ID FROM userinfo WHERE username = '".$username."' ";
$resultCheck = mysqli_query($conn,$sqlCheck);
if($resultCheck){
if(mysqli_num_rows($resultCheck)>0)
{
$sqlUpdate = "UPDATE userinfo SET {$day} = '".$value."' WHERE username = '".$username."' ";
$resultUpdate = mysqli_query($conn,$sqlUpdate);
if($resultUpdate)
{
echo("Update success");
}else
{
echo("Update Failed");
}
}
}else{
echo("There was an Error.");
}
}
我不太清楚为什么,但这对我来说非常有效。
关于c# - PHP 中的 SQL 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44787326/