- Home /
Player score update with PHP MySQL
I have a users table where I want to update the scores each time a user finishes the game. Unityscript part is working fine but after I post the score to the database it appears doubled or tripled. I post the score as int and also the table column is of int format. My PHP looks like this:
$username = ($_POST['username']);
$score = ($_POST['score']);
try {
$db = new PDO("mysql:host=$host; dbname=$dbname", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $db -> prepare ("UPDATE users SET score = score + $score
WHERE username = :username");
$statement->execute(array(':username' => $username));
}
catch(PDOException $e) {
echo $e->getMessage();
}
Any help or advice is appreciated.
Answer by HarshadK · Nov 05, 2014 at 02:09 PM
Because you are adding the current score to the previous score in your query by using
SET score = score + $score
which will add the current score to the previous score that was already present in the database.
Yes I know but this is what I want to achieve. But for some reason it doesn't just add it but it adds it doubled or tripled.
Then reason for that is your PHP script is running multiple times, one reason might be you are sending score to your PHP script multiple times. Just print out the time at which each score was added so you'll come to know if there are multiple entries sent.
Your answer
Follow this Question
Related Questions
Saving a leaderboard using PHP and C#. 2 Answers
Unity to PHP/MySQL: password and username security 1 Answer
Javascript with php 1 Answer
Android Mysql Database online 3 Answers
Using PHP to generate objects or list in Unity with C# 2 Answers