c# - PHP 中的 SQL 更新

标签 c# php mysql sql unity-game-engine

我对 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/

相关文章:

c# - 如何将声明性语法中 "OnDataBound"的 "DropDownList"事件绑定(bind)到其他类中的静态方法?

php - 在 AJAX 页面中获取数据

MySQL:为什么 GROUP_CONCAT 在我的多连接查询中丢失行?

mysql - mariadb 外键非选择性索引

php - Laravel 5 PDO 错误

c# - 当 c# 访问 c++ dll 时尝试读取或写入 protected 内存错误

c# - 选择静态选择列表的值

c# - 表单完全可见后如何正确加载 DataGridView?

php - 字符编码错误

php - 哪种类型的 ip 地址是 "2405:204:a581:e021:995b:368e:9b00:ddd5"?