[英]SQL updates in PHP
I'm really new into PHP and SQL and I'm trying to learn how to use them with Unity. 我真的是PHP和SQL的新手,我正在尝试学习如何在Unity中使用它们。
First I created a new table in my database with Unity which worked OK. 首先,我使用Unity在数据库中创建了一个新表,该表工作正常。 Now I want to update this table with some info, this info should come from Unity. 现在,我想用一些信息更新该表,该信息应该来自Unity。 But it's not working; 但这不起作用; I'm not sure if it's my C# script or my PHP script. 我不确定这是我的C#脚本还是我的PHP脚本。 First, here's a snippet from my C# script: 首先,这是我的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!");
}
}
and now my PHP script 现在是我的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.";
?>
I just want to send in a specific table at a specific day a number. 我只想在特定日期发送特定表中的数字。
I re-thought my work. 我重新考虑了我的工作。 So no table for every user, only on Table with all user/userinfos and thinks a want to save/load. 因此,每个用户都没有表,只有所有用户/用户信息都在表上,并认为要保存/加载。
C# script is more or less the same: C#脚本大致相同:
IEnumerator InsertMemberData(string username,string day,int sale)
to 至
IEnumerator InsertMemberData(string username,string day,**string** sale)
and my PHP script : 和我的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.");
}
}
I'm not really sure why, but this worked perfect for me. 我不太确定为什么,但这对我来说很完美。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.