Save data to mysql
This is my script in php:
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); //la primera var es la posicion relativa de la ddbb, nombre y paswword en las sgtes
//check that conection happenned
if(mysqli_connect_errno()) //check or si pasaron algunos errores, si es true esq hubieron errores.
{
echo "1: conection failed"; // error code #1 = connection failed
exit();
}
$username = $_POST["name"];
$newscore = $_POST["score"];
$namecheckquery = "SELECT username, score FROM players WHERE username = ' " . $username . " ' ";
$namecheck = mysqli_query($con, $namecheckquery) or die("2: name check query failed"); // error code #2 = name check query failed
if(mysqli_num_rows($namecheck) != 1)
{
echo "5: Either no user with name or more than one"; // error code #5 number of names maching !=1
exit();
}
$updatequery = "UPDATE players SET score = ".$newscore." WHERE username = '".$username."' ";
mysqli_query($con, $updatequery) or die ("7: Save query failed");
echo "0";
This my script in C#:
public Text playerDisplay;
public Text scoreDisplay;
// Start is called before the first frame update
void Awake()
{
if(DBManager.username == null)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
playerDisplay.text = "Player : " + DBManager.username;
scoreDisplay.text = "score: " + DBManager.score;
public void CallSaveData()
{
StartCoroutine(SavePlayerData());
}
IEnumerator SavePlayerData()
{
WWWForm form = new WWWForm();
form.AddField("name", DBManager.username);
form.AddField("score", DBManager.score); //
WWW www = new WWW("http://localhost:8888/sqlconnect/savedata.php", form);
yield return www;
if(www.text == "0")
{
Debug.Log("Game Saved");
}
else
{
Debug.Log("Save failed. Erro # " + www.text);
}
DBManager.LogOut();
SceneManager.LoadScene(0);
}
public void IncreaseScore()
{
DBManager.score++;
scoreDisplay.text = "score: " + DBManager.score;
}
I am getting game saved but when I login again the score is still '0' and the score also did not update in my table and I don't know why.
Comment
Your answer
Follow this Question
Related Questions
2D EndlessRunner Spike Placing 0 Answers
Unity3D connection to MySQL error 1 Answer
unity3d 5 game on fullscreen shows only half of game screen 4 Answers
error CS0201 1 Answer
How do I show ranking images at winning post in unity3d? 0 Answers