【问题标题】:Send variable from C# to php for MySQL (Unity)将变量从 C# 发送到 MySQL (Unity) 的 php
【发布时间】:2017-03-02 14:55:46
【问题描述】:

我是 MySQL 系统的新手。我必须存储在数据库中:

用户名; 分数; 资源。

对于发送用户名、分数和资源,我没有任何问题,但是当我尝试从数据库中获取资源时出现此错误:

</style>
</head>

<body>
<h1>Bad request!</h1>
<p>


Your browser (or proxy) sent a request that
this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
<a href="/">localhost</a><br />
<span>Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1</span>
</address>
</body>
</html>


UnityEngine.Debug:Log(Object)
<SetPlayerName>c__Iterator1:MoveNext() (at       Assets/Scripts/DataBase/MySQL/DBLoader.cs:67)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

这是我的 php 文件:

<?php

    $servername = "host";
    $username    = "name";
    $password    = "";
    $dbname        = "game_name";

    $coinsname     = (isset($_REQUEST['coinsnameGet']) ? $_REQUEST['coinsnameGet'] : null);

    //Create Connection
    $connection = new mysqli($servername, $username, $password, $dbname);

    //Check Connection
    if(!$connection) {
        die("Connection Failed. " . mysqli_connect_error());
    }

    $sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
    $result = mysqli_query($connection, $sql);

    while($row = mysqli_fetch_array($result, MYSQLI_BOTH) {
        echo $row['Coins'];
    }
?>

我必须检查名称是否 == 到本地数据库名称然后获取资源。 本地数据库是用 sqlite 制作的。

这是我的连接类:

private DataBase dbLocal;

    WWW userData;
    WWW scoreData;
    WWW coinsData;

    public Text Coins;

    private string[] dbUsers;
    private string[] dbScores;

    private string dbCoins;

    private string postCoinsData = "http://host/game_name/UsercoinsData.php";

    public GameObject scorePrefab;
    public Transform scoreParent;

    public GameObject rankValue;
    public GameObject nameValue;
    public GameObject scoreValue;

    // Use this for initialization
    IEnumerator Start () {

        dbLocal = (DataBase)FindObjectOfType(typeof(DataBase));

        userData = new WWW("http://host/game_name/UsernameData.php");
        scoreData = new WWW("http://host/game_name/UserscoreData.php");

        yield return userData;
        yield return scoreData;

        string textUserData = userData.text;
        dbUsers = textUserData.Split(';');

        string textScoreData = scoreData.text;
        dbScores = textScoreData.Split(';');

        scoreParent.transform.localPosition = new Vector3(0, 0, 0);

        dbLocal.Connection();

        StartCoroutine(SetPlayerName());
        GenerateScore();
    }

    IEnumerator SetPlayerName()
    {
        string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());
        WWW dataCoins = new WWW(setName);

        Debug.Log(dbLocal.GetName());

        yield return dataCoins;

        string textDataCoins = dataCoins.text;
        Coins.text = textDataCoins;
        Debug.Log(Coins.text); //This log the error
    }

如果我改变这个:

$sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";

用这个:

$sql = "SELECT Coins FROM score WHERE Name = 'PlayerName'";

在浏览器中我看到了分数。

提前谢谢!

【问题讨论】:

  • 您对SQL Injections 持开放态度,应该真正使用Prepared Statements 而不是连接您的查询。特别是因为您根本没有逃避用户输入!这不仅仅是一个安全问题,而且如果输入包含例如' 字符或反斜杠,您的查询将失败。

标签: c# php mysql sqlite unity3d


【解决方案1】:

您的 php 将永远找不到 $_REQUEST['coinsnameGet'],因为您正在创建这样的 URL:

string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());

看起来像:

http://host/game_name/UsercoinsData.phpcoinsnameGet = 玩家名

但应该看起来像

http://host/game_name/UsercoinsData.php?coinsnameGet=PlayerName

不完全确定这是导致您的问题的原因还是复制粘贴错误,但如果是,您的代码应更改为:

string setName = postCoinsData + "?coinsnameGet=" + WWW.EscapeURL(dbLocal.GetName());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2017-04-26
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多