c# - 无法在我的网站上注册用户(PHP、Unity3D)

标签 c# php mysql unity-game-engine

我有一个网站,我想用它来存储我在 Unity3D 中制作的多人游戏的数据(如用户名、密码等)。

现在我想使用C#和Unity3D注册某人(将用户名和密码添加到MySQL数据库)。我使用 WWW 类与网站进行通信。在我看来,我在 Unity3D 中编写的代码工作正常,但是当我访问 phpMyAdmin 并搜索新注册用户时,我找不到它。所以我想,问题一定出在PHP脚本上,但我找不到它。

问题又是用户名和密码没有添加到 MySQL 表中。

我将发布这两个脚本,尽管我认为 Unity3D 中的脚本工作正常。请注意,我是 PHP 的绝对初学者。

<?php

$mysql_host = "example.host.com";
$mysql_database = "example_database";
$mysql_user = "exampleuser";
$mysql_password = "toomanysecrets";

try
{

$toQuery = 'SELECT name,
                password
            FROM basicUserInfo
            ORDER BY name';

if ($_POST["name"] || $_POST["password"])
{
    $conn = new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user,     $mysql_password);
    echo "Connected to $mysql_database at $mysql_host successfully!";
    $forQuery = 'INSERT INTO  `a1936371_userinf`.`basicUserInfo` (
                `name` ,`password`
                )
                VALUES (
                    \'$_POST[\'name\']\',  \'$_POST[\'password\']\'
                    )';

        if (!$conn->query($forQuery))
        {
            echo "Failed to add data!";
        }
        else
        {
            echo "Added data sucessfully";
        }
    //Close the connection
    $conn = null;
}

} catch (PDOException $pe)
{
   die("Could not connect to the database $mysql_database:" . $pe->getMessage());
}

Unity3D:

void Update()
{
    if (Input.GetKeyDown(KeyCode.S))
    {
        Register("testt", "test1");
    }
}

public void Register(string playername, string password)
{
    StartCoroutine(RegisterOnServer(playername, password));
}

IEnumerator RegisterOnServer(string playerName, string password)
{
    string url = "http://deathrunserv.net16.net/index.php";

    WWWForm form = new WWWForm();
    form.AddField("name", playerName);
    form.AddField("password", password);

    WWW www = new WWW(url);
    yield return www;
    Debug.Log("Done!");

}

最佳答案

在您的 C# 代码中,您没有在任何地方使用 WWWForm。尝试:

WWWForm form = new WWWForm();
form.AddField("name", playerName);
form.AddField("password", password);

WWW www = new WWW(url, form);
yield return www;
Debug.Log("Done!");

关于c# - 无法在我的网站上注册用户(PHP、Unity3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850163/

相关文章:

c# - 结果不正确,我做错了什么?

java - 如何沙盒类?

php - 创建评论 anchor

PHP 函数检索 mysql 表列不起作用

mysql - 无法在mysql上创建外键

Mysql Distinct函数嵌套在select中的其他函数中

c# - 创建一个暂停的触发器

c# - 多个串行端口读取仅在几分钟后挂起

C# 控制台 : How to make the console stop scrolling automatically?

php - CodeIgniter 上传库不返回文件扩展名