Jump to content

Query Math


russellmac

Recommended Posts

I have an innoDB that gets updated often(several times a second) recently I've noticed that when a row gets updated, sometimes instead of doing the math i have defined outside the query it is doubling the exsiting row. for example

$newnumber = $user->points + $amount;
$result = mysql_query("UPDATE `users` SET `points` = '".$newnumber."' WHERE `id`='".$user->id."'");

 

normally it works fine. Say they had 5000 points and $amount was 6 then it would update to 5006 however sometimes it updates to 10012!

I'm thinking that it is possible that two queries maybe running into each other or something and getting added at the same time??

Would it be better to do the math inside the query like this?

$result = mysql_query("UPDATE `users` SET `points`=`points`+'".$amount."' WHERE `id`='".$user->id."'");

Link to comment
Share on other sites

I don't think something basic like that can be found by searching, you just set a session variable once your code has processed the request and then test at the start of your code is that session variable is already set and skip the processing if it is.

 

<?php
session_start();
// determine if the form processing code has been executed once
$processed = isset($_SESSION['processed']) ? TRUE : FALSE;

if(!$processed) {
    // do your normal form processing here

    // at the point that the form data has been completely and correctly processed, set the session variable to indicate not to process it again during the current session -
    $_SESSION['processed'] = TRUE;
}
?>

Link to comment
Share on other sites

OK i will give that a try thanks. Also had a follow up question about Ken2k7 suggestion to remove the sing quotes... would it even be better to just remove all quotes from the variable like this?

$result = mysql_query("UPDATE `users` SET `points`=`points`+$amount WHERE `id`= ".$user->id);

I tried it and it still processed ok, I was just wondering what the benefit of it is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.