通过在列中插入相同的值来“插入SQL”

时间:2017-10-24 00:07:21

标签: php mysql sql

我有一个自动投标系统的投标系统,还有一个客户可以投标的按钮。

当客户端单击该按钮时,它会调用执行以下查询的函数:

$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 . ")";

此查询位于PHP文件中。

机器人的一部分由数据库中的过程完成,使用以下命令,该命令每秒运行一次:

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); 

投标与计时器一起运行,等于美分的拍卖。发生的问题是,如果客户在机器人提供出价的同时点击按钮,它会以相同的值写入数据库。

我需要它将不同的值写入“ bidding_price ”列,如下所示:

我需要这样写:

robot 0.25
customer 0.26

而不是这样:

robot 0.25
client 0.25

我不想把所有代码放在这里,因为它太长了,所以我把最重要的部分放在一边。

$q = "select * from auc_due_table adt left join auction a on adt.auction_id=a.auctionID left join auction_management am on a.time_duration=am.auc_manage where auction_id=$aid";
$r = mysqli_query($db, $q);
$ob = mysqli_fetch_object($r);

$oldtime = $ob->auc_due_time;
$oldprice = $ob->auc_due_price;
$plusprice = $ob->auc_plus_price;
$plustime = $ob->auc_plus_time;
$newprice = $ob->pennyauction == 1 ? $oldprice + 0.01 : $oldprice + $plusprice;
$newtime = $oldtime < $plustime ? $plustime : $oldtime;

2 个答案:

答案 0 :(得分:0)

也许这个:

   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', 
                     (SELECT max(bidding_price)+0.01 FROM bid_account WHERE auction_id=auctionID) AS bidding_price,                     
                     's', 
                     auc_due_time 
             FROM 
                     t_autolances
             LIMIT 1

答案 1 :(得分:0)

您需要在SQL中添加用户的金额,而不是在PHP中。

$qryins = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time)
            SELECT '$uid', NOW(),'1', auction_id, '$prid','d', 
                    auc_due_price + IF(pennyauction, 0.01, auc_plus_price), 's', auc_due_time
            from auc_due_table adt 
            left join auction a on adt.auction_id=a.auctionID 
            left join auction_management am on a.time_duration=am.auc_manage 
            where auction_id=$aid";

另一种解决方案是使用交易。在SELECT查询之前启动事务,并在INSERT之后提交。

mysqli_query($db, "START TRANSACTION");
$r = mysqli_query($db, $q);
$ob = mysqli_fetch_object($r);
$oldtime = $ob->auc_due_time;
$oldprice = $ob->auc_due_price;
$plusprice = $ob->auc_plus_price;
$plustime = $ob->auc_plus_time;
$newprice = $ob->pennyauction == 1 ? $oldprice + 0.01 : $oldprice + $plusprice;
$newtime = $oldtime < $plustime ? $plustime : $oldtime;
$qryins = "...";
mysqli_query($db, $qryins);
mysqli_query($db, "COMMIT");