nightkarnation Posted April 13, 2011 Share Posted April 13, 2011 Hello Guys, I would really appreciate some feedback on this one. Here's the basic simplified code for my bidding system site (querys for when a user sends a new valid bid): Its working fine but if 2 users send a bid at exactly the same precise moment (even same milliseconds), I have 2 problems (described below code, for better understanding) Please picture this code as if its happening 2 times at the same time (from 2 different users) $time = microtime(true) * 10000; $result = mysql_query("UPDATE `AUCTIONS` SET Current_Price = Current_Price + '$increase_price', Bids = Bids + 1, Bidder = '$bidder', Bidder_Time = '$bidder_time' WHERE PId = '$id'"); //grab the current price to save on bidding history DB if($result) { $resultPrice = mysql_query("SELECT Current_Price FROM AUCTIONS WHERE PId = '$id'"); while($row=mysql_fetch_array($resultPrice)) { $current_price = $row['Current_Price']; } if($resultPrice) { $result = mysql_query("INSERT INTO `BIDDING_HISTORY_DB` (Id, Bidder, Bidder_Time, Raw_Time, Ip, Type, Bid_Amount) VALUES ('$id', '$last_bidder', '$last_bidder_time', '$time', '$client_ip', '$credit_type', '$current_price')"); } } If 2 users bid at exactly the same SAME moment, its possible that on BIDDING_HISTORY_DB... I get the Bid_Amount field with the same value (when it should be always 1 number higher than the previous) ( Bid_Amount is coming from $current_price) $current_price grabs the new updated value from the AUCTIONS DB on the first query (Current_Price = Current_Price + '$increase_price'). So its impossible to have 2 values that are the same (at least I am not expecting that), but I realized that if the query from 2 users are being processed at the same time and the UPDATE AUCTIONS query is processed 2 times and AFTER THAT then the SELECT Current_Price FROM AUCTIONS WHERE PId = '$id' is processed on both bids...that is when the Current_Price is grabbed with the same value for both USERS!! Any ideas on how I can find a solution to this ??? The other problem is that I am knowing which bid was made (by which user) before/after because of the $time variable. But sometimes when 2 bids are made at the SAME precise time with the same milliseconds (yes it happens) ... this is when problems arise (because 1 user could have as Current_Price 10.11 and the other one 10.12 and then on DB the user with 10.12 could be shown as if he made the bid first, any ideas/suggestions on how I can find a solution to this? Quote Link to comment https://forums.phpfreaks.com/topic/233644-bidding-system-when-2-users-bid-at-the-same-time-what-to-do-suggestions/ Share on other sites More sharing options...
maxudaskin Posted April 13, 2011 Share Posted April 13, 2011 First off, it is so extremely unlikely that two people would put a bid in at the same time, but if you really want to prevent it from happening, check the database for any new bids by other users immediately before posting the current user's bid. If there are new bids, put a bid place holder in with the next bid price up, and ask if they want to increase the bid to what is in the placeholder. All the while, new bids may be placed, so it should be a reoccurring function. Beginning of Function [user Submits Bid] (Check for new bids since page load) - If new bid: (Place Bid Placeholder) [Get user input] - If Accept (Recall function) - Else (Return to previous page or what ever you want it to do) - Else (Place Bid) Quote Link to comment https://forums.phpfreaks.com/topic/233644-bidding-system-when-2-users-bid-at-the-same-time-what-to-do-suggestions/#findComment-1201297 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.