Problem with SQL (duplicate addition)

Asked

Viewed 50 times

0

I have a bidding system that works with a robot giving automatic bids, and also with a button where the customer can bid.

When the client clicks on the button, it calls a function that executes the following query:

$qryins = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time)
                values('$uid',NOW(),'1','$aid','$prid','d',$newprice,'s'," . $oldtime . ")";

The robot part is made by a process in the database, with the following command:

Insert into bid_account (user_id, bidpack_buy_date, bid_count, auction_id, product_id, bid_flag, bidding_price, bidding_type, bidding_time)
        SELECT prox_user_id, NOW(), '1', auctionID, productID, 'd', prox_valor, 's', auc_due_time FROM t_autolances
        WHERE prox_valor NOT IN (SELECT bidding_price FROM bid_account WHERE auction_id=auctionID); 

The bids are executed with a clock, like those dime auctions. The problem that is occurring is that if the client clicks on the button at the same time as the robo gives the bid, it writes to the database with the same values.

For example ta chronometer in 2s, at that time the robot will give the bid and the customer also gave. By checking the bid_account table you can see that the values were duplicated, all with the same information.

How can I get him to record one at a time? In fact what I need is for him to record the different value only for the column bidding_price, because it has the values as for example: 0.25

I need it recorded that way:

robo 0.25
cliente 0.26

and not like this:

robo 0.25
cliente 0.25

I didn’t want to put all the code here because it’s too long, so I put the most important parts.

  • Sorry to ask, but the robot exists to inflate the bids ?

  • @Motta the robot exists to reach the minimum price of a product.

  • ok, in this case I would also study a "lock table" https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-triggers.html

  • What would it be like? If you can give me some example with the same query, why am I not aware of that part.

  • I don’t know Mysql to the point of doing the syntax , just from a database concept.

1 answer

0

The best solution would be to use last_id_inserted and then use this id to query existing relations.

   $query =" Insert into bid_account (user_id, bidpack_buy_date, bid_count, auction_id, product_id, bid_flag, bidding_price, bidding_type, bidding_time)";
$result = mysqli_query($link, $query);

if($result){

 $last_id = mysqli_insert_id($link);

$query_two =" SELECT prox_user_id, NOW(), '1', auctionID, productID, 'd', prox_valor, 's', auc_due_time FROM t_autolances
        WHERE prox_valor NOT IN (SELECT bidding_price FROM bid_account WHERE id='$last_id')"; 

}
  • I didn’t understand very well, would it work? Because they would be executed at exactly the same time, there is no way to check one before.

  • Remembering that each query is separate from each other, one stays in a database database and the other in a PHP file.

  • How about you do it like this, you make the robot or the client one of them have priority in the ID, this way he’ll select a winner, and the competitor is pending. Create an array with pitch and an array for winner and compare the bids through the value it inserts, time and also the user type, whether it is robot or client.

  • The winner is only selected when the timer zeroes, and this cannot happen without counting the two bids, in the case of the robot and the customer.

  • I’ve already given the answer to your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.