无法在我的网站(PHP, Unity3D)注册用户
本文关键字:Unity3D 注册 用户 PHP 我的 网站 | 更新日期: 2023-09-27 18:13:38
我有一个网站,我想用它作为一个地方,我可以存储数据(如用户名,密码等),我在Unity3D制作的多人游戏。
现在我想注册某人(添加用户名和密码到MySQL数据库)使用c#和Unity3D。我使用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!");