Oliver Sradnick Oliver Sradnick - 1 month ago 8
C# Question

Unity, PHP, Sql Update

Im realy new into PHP,SQL and im trying to learn How to with Unity.

First i created a new Table in my Database with Unity wich worked Ok.
Now i want to Update this Table with some infos,these infos should come from Unity.
But its not working im not sure if its my C# script or my PHP script.
Add First a snipped from my C# script

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

$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.

Answer Source

I rethinked 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..

IEnumerator InsertMemberData(string username,string day,int sale)

to

IEnumerator InsertMemberData(string username,string day,**string** sale)

and my PHP script :

    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.");
    }   
}

Im not realy sure why, but this worked perfect for me. the for the Help @Fred -ii-