views:

201

answers:

4

I am trying to add 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.

A: 

It doesn't look like it should be. Is it possible that the SQL query is run twice?

mopoke
+1  A: 

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

DeveloperChris
+1: You beat me to it - print out the statement prior to execution.
OMG Ponies
cool Thanks I'm a teacher now. :) I just wanted to add, be very very sure you know what you are doing when writing sql statements like the above. you may open yourself to sql injection attacks and a whole world of hurt.
DeveloperChris
A: 

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.

Kaleb Brasee
A: 

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.

James K
I already did it, but you can edit your question (or answer for that matter) by clicking the edit link. It's to the left of your username, between link and flag. You might consider deleting this, as some will downvote it because it's not an answer. The delete link is in the group along with edit/flag/etc.
OMG Ponies
James, given your basic setup, one way to improve duplicate prevention is to have a transaction table (for credit adjustments) which would includes a page token value. A token is generated per page request and has a corresponding column in the credit transaction table where it gets inserted per credit adjustment. Each ajax post (every 5 seconds) always sends the same token for the lifetime of that page so only one credit adjustment can occur per page rendering by checking there is no matching token in the table).
micahwittman