Check if the users balance is higher than the bid before going through

hi so am having an updated issue here, problem is when the admin makes changes to reflect a user account (balance), it doesnt change unless the user refreshes his/her webpage, only then can the code return error balance too low, but if not refresh the form get submitted.
this an issue cause a user can have an high amount bidding on a lower bid,
eg user account: 100p
item 50p

but when the user account is deducted to 40p, users page isnt updated unless they refresh, this gives the user the opportunity to bid on the item which means the admin now in debt

in straight words am looking to get the balance value

1 automatically updated on change from the database or
2 code to check if the users balance is higher than the bid before going through else return error
3. server check user row every x sec and update value front end or so
something like this just a rough of what my mqsl looks like

if ($balance < $bid){
    $query = "SELECT u.balance, b.bid FROM " . $DBPrefix . "bids 

WHERE balance < :bid and auction = :auc_id";
$params[] = array(':bid', $bid, 'int');
$params[] = array(':auc_id', $auc_id, 'int');
$db->query($query, $params);
if ($db->numrows() > 0)
    {
    $errmsg = $ERR_097;
    }
    }

tables here are users which holds the balance of the user and bids which holds the amount of the item
TAble users column balance
table bids column bid

hope u understand, thanks.

If you want to have the user able to sit with a page open but have their balance on screen dynamically update when something else happens on a different screen, as far as I know the only way is to use Javescript to query the database after a period of time. That period of time is something you’ll have to decide based on what you think is reasonable. You can then call a JavaScript function that will query the server for the new balance. This is basically Ajax, similar to having code that queries the server when you’ve chosen from a drop-down list but before you submit the form.

But, even if you implement this, you still need to do option 2, which is to ensure at the time they place a bid that they have the balance to do so, and return to the “place bid” screen with a message if they do not. That’s because there’s always a chance that the balance will change in the short time between them pressing the button to place a bid, and the server script kicking in to process it.

yeah i think option two is much better too but i dont seem to know how to get it done the only code i could write only return error if the page is already refreshed and the balance is lower than the bid


// if balance lower than bid
        if (bccomp($balance, $Data['minimum_bid']) == -1) {
            return '6077';
        }

but if the page isnt refreshed the form get submitted

Yes, but when the form is submitted, that’s the place to do a balance check. Just add it in before the bid is processed. It’s very unsafe to assume that if the user has a link to process a bid that nothing has changed elsewhere.

Really what you need to do at that point is to lock the user somehow, because even if your form submission checks the balance and then processed the bid, there’s still a chance that a second bid might come in between the point where the first balance check is done, and the bid processed and balance reduced, which would incorrectly be processed. I think that’s where SQL transaction processing comes into play, but I don’t know much about it. At the very least, you need to check the balance and, if the bid can be processed, update the balance as quickly as possible.

But what you really need is to prevent any other process from nipping in between those two and starting to process another bid from the same user. If you consider the steps taken to process the bid you can minimise how long between the balance being checked and the bid being processed, but you really need to eliminate it entirely.

ok but see this i its a already exist caode

$query = "SELECT tagged FROM " . $DBPrefix . "bids WHERE tagged = :tagged and auction = :auc_id";
    $params = array();
    $params[] = array(':tagged', $tagged, 'int');
    $params[] = array(':auc_id', $id, 'int');
    $db->query($query, $params);
    if ($db->numrows() > 0)
    {
    $errmsg = $ERR_0099;
    }

now even if the user doesnt refresh it page and submit a form, that above returns with error saying that user already been tagged, then i beleive there could be a way to write to check the users balance and if its below minimum_bid return an error.

Tables involved

table column
users id balance
auctions id minimum_bid

hoping the above cold could be rewritten to check user balance is higher than minimum_bid when biding on in item

I can’t see the connection between the code you posted, and comparing the balance to the minimum bid. You’d need to run a separate query before you run the code in your other topic.

yeah i just ended up doing it comparing both column it i have multiple error checks on balance alone,

$query = "SELECT balance FROM " . $DBPrefix . "users WHERE id = :user_id and balance < :bid";
$params = array();
$params[] = array(':user_id', $user->user_data['id'], 'int');
$params[] = array(':bid', $bid, 'int');

that seem to make it happen still checking on bugs thou

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.