Oliver Sradnick Oliver Sradnick - 1 year ago 151
SQL Question

SQL updates in PHP

I'm really new into PHP and SQL and I'm trying to learn how to use them with Unity.

First I created a new table in my database with Unity which worked OK. Now I want to update this table with some info, this info should come from Unity.
But it's not working; I'm not sure if it's my C# script or my PHP script.
First, here's a snippet 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 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:

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

I'm not really sure why, but this worked perfect for me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download