user3453366
user3453366

Reputation: 51

AJAX Post with Integers

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.

Upvotes: 0

Views: 130

Answers (3)

TimC
TimC

Reputation: 1

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.

Upvotes: 0

Jono20201
Jono20201

Reputation: 3205

http://www.php.net/manual/en/function.intval.php

or typecast

$something = (int) $var * 5;

Upvotes: 0

Radu-Stefan Zugravu
Radu-Stefan Zugravu

Reputation: 99

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?

Upvotes: 1

Related Questions