I am using AJAX to post to a highScores table in a sqlite database, the total of the high score has to be calculated by a query and retrieved back through the database. How can I send the score through AJAX to the PHP script as an integer so I can perform mathematical calculations on it? St the moment I cant seem to do anything with it because the value gets to the php script as a string.
Asked
Active
Viewed 129 times
0
-
Ever hear of typecasting? http://www.php.net/manual/en/language.types.type-juggling.php – va5ja Apr 01 '14 at 14:21
-
In php try this `$num = (int) $_POST["yourIntegerAsString"];`. Check http://stackoverflow.com/questions/8529656/php-how-to-convert-string-to-number – Aamir Afridi Apr 01 '14 at 14:22
-
I have tried to cast it with the use of (int) but still nothing happens – user3453366 Apr 01 '14 at 14:28
-
Remove the quotes in your query. `'$socre'` should be `$score` because it is an integar – A.Essam Apr 01 '14 at 14:34
3 Answers
1
PHP is a loosely-typed language so you can easily cast the received value. See the following answer for more information: How do I convert a string to a number in PHP?

Community
- 1
- 1

Radu-Stefan Zugravu
- 99
- 9
0
Shouldn't you just remove the single quotes around your $score variable?
instead of:
$query = "UPDATE User SET Score = '$score' + Score WHERE Username = '$player'";
this:
$query = "UPDATE User SET Score = $score + Score WHERE Username = '$player'";
You do need to do your own sql-injections checks.
Otherwise you'll make it a string in your sql-query.

TimC
- 1
-
You don't know that he doesn't want the string '$score' as a part of the value being updated. – Adam Zuckerman Apr 01 '14 at 14:59