mysql query adding to a decimal doubles value of the addend for some reason
I am trying to ad开发者_如何学God a value ($credit
) to the value of a decimal (9,2 max length) field in a mysql database. The problem is that when I do this, it doubles the $credit
and then adds it for some reason. I just want it to add it. The code I am using is:
mysql_query("update $table set earnings = earnings +$credit where username = ('$member')");
I want to add the value of $credit
to the field called earnings. Does somebody know why it is doubling the value of $credit
before adding it?
Update
I echoed the query and I saw that the value that it was adding was indeed the right value of $credit. And then I checked the database under earnings and it had added the right value. I don't know why it automatically fixed itself. I think it's possible that it used to run twice because I have the script running on ajax every 5 seconds with prototype. After $credit is added successfully I make an entry into another database saying that it was added and then before I have:
if(database says that the credit was added) {
exit;
}else{
run the add $credit query
So maybe, the script wouldn't have enough time to finish the whole thing in 5 seconds so it would never tell the other database that the $credit addition was complete so it would add it again and then for some reason have enough time to write it completed it the second time? I don't know, but thanks anyways for the help.
I echoed the query and I saw that the value that it was adding was indeed the right value of $credit. And then I checked the database under earnings and it had added the right value. I don't know why it automatically fixed itself. I think it's possible that it used to run twice because I have the script running on ajax every 5 seconds with prototype. After $credit is added successfully I make an entry into another database saying that it was added and then before I have
if(database says that the credit was added){ exit;
}else{ run the add $credit query
so maybe, the script wouldn't have enough time to finish the whole thing in 5 seconds so it would never tell the other database that the $credit addition was complete so it would add it again and then for some reason have enough time to write it completed it the second time? I don't know, but thanks anyways for the help.
Do this to see what happens
$sql = "update $table set earnings = earnings +$credit where username = ('$member')";
echo $sql;
mysql_query($sql);
And see what the echo produces.
Take it from there.
DC
Not being able to comment on anything but my own stuff is frustrating. That be as it may...
If the javascript runs every 5 seconds you may find it runs twice in a much shorter time than that. javascript is a hit and miss affair when it comes to timer accuracy. so don't rely on it.
Also as javascript is client side, so someone could inflate their earnings by just calling the function every second or 20 times a second. My comment below about security is more important than ever. I certainly hope you don't pay anyone real money based on their earnings.
If you do can I have the url please ;)
DC
It doesn't look like it should be. Is it possible that the SQL query is run twice?
Your update statement looks correct. I'd add some debug messages to make sure that the value of $credit is correct prior to the update, and also to make sure that the update is only running one time.
精彩评论