PHP如果运行两次并生成重复的记录到MySQL数据库

时间:2017-06-22 10:05:28

标签: php mysql sql pdo

我正在为一款游戏编写一小段代码,用户可以在该游戏中出价并获得最高出价(如拍卖)。有一个1分钟的计时器,当它结束时,会发生一些动作。我将在下面写下所有细节。

计时器脚本:

$auctionTimer = 1;

$target_time = strtotime('+' . $auctionTimer .  'minutes', strtotime($r1['date']));
$current_time = time();
$seconds_left = $target_time - $current_time;
$minutes_left = floor($seconds_left / 60);

if (strlen($minutes_left) != 2) {
  $minutes_left = sprintf("%02d", $minutes_left);
}  

$seconds_left -= $minutes_left * 60;
$seconds_left = floor($seconds_left);

if (strlen($seconds_left) != 2) {
  $seconds_left = sprintf("%02d", $seconds_left);
}

if ($current_time > $target_time && $ragr1['count'] >= 1) {检查有点不对:

$stmt1 = $pdo->prepare("SELECT id, user_id, user, bid, date FROM 
    tb_auction_bids ORDER BY id DESC");
$stmt1_while = $pdo->prepare("SELECT id, user_id, user, bid, date FROM 
    tb_auction_bids");
$stmt3 = $pdo->prepare("SELECT id, user_id FROM tb_auction_winners ORDER BY 
    id DESC LIMIT 1");
$stmt6 = $pdo->prepare("UPDATE tb_users SET money = money + :win WHERE id = 
    :user_id");
$stmt9 = $pdo->prepare("INSERT INTO tb_auction_winners (user_id, user, win) 
    VALUES (:user_id, :user, :win)");
$stmt11 = $pdo->prepare("SELECT id, user_id, user FROM tb_auction_bids ORDER 
    BY id DESC LIMIT 1");
$stmt12 = $pdo->prepare("TRUNCATE TABLE tb_auction_bids");

$agr1 = $pdo->prepare("SELECT COUNT(id) AS count FROM tb_auction_bids");
$agr2 = $pdo->prepare("SELECT SUM(bid) AS sum FROM tb_auction_bids");


$stmt1->execute();
$stmt1_while->execute();
$stmt3->execute();
$stmt11->execute();

$agr1->execute();
$agr2->execute();

$r1 = $stmt1->fetch(PDO::FETCH_ASSOC);
$r3 = $stmt3->fetch(PDO::FETCH_ASSOC);
$r11 = $stmt11->fetch(PDO::FETCH_ASSOC);

$ragr1 = $agr1->fetch(PDO::FETCH_ASSOC);
$ragr2 = $agr2->fetch(PDO::FETCH_ASSOC);

if ($current_time > $target_time && $ragr1['count'] >= 1) {
  $stmt9->execute(array(
    ':user_id' => $r11['user_id'],
    ':user' => $r11['user'],
    ':win' => $ragr2['sum']
  ));
  $stmt6->execute(array(
    ':win' => $ragr2['sum'],
    ':user_id' => $r1['user_id']
  ));
  while ($r1 = $stmt1_while->fetch(PDO::FETCH_ASSOC)) {
    $stmt7->execute(array(
        ':user_id' => $r1['user_id'],
        ':aukciono_id' => $r3['id'],
        ':user' => $r1['user'],
        ':date' => $r1['date'],
        ':bid' => $r1['bid']
    ));
  }
  $stmt12->execute();
}

我想我不应该发布PDO查询,因为问题不在于SQL,而在于PHP。如果你不在这个页面(... /拍卖,例如),当1分钟计时器结束时,它什么都不做。您必须再次参加... /拍卖,例如获得奖励。我应该使用JavaScript来检查计时器是否结束然后执行文件吗?

这是它的样子(视频): https://www.youtube.com/watch?v=_CZcnd3RTNw

另外,我得到了

  

“SQLSTATE [23000]:违反完整性约束:1048列'发布'   不能为空“错误,我调查了这个帖子:   SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post' cannot be null

但它没有帮助。 $r1['aukciono_id']也被定义为另一个变量,但只有$r1['aukciono_id']给出错误。

if ($current_time > $target_time && $ragr1['count'] >= 1) {应该只运行一次但运行两次并生成重复的MySQL记录。也许是因为那个PDO错误?有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我自己想出了问题:)谢谢你的想法,问题在于while循环。我应该使用另一个查询。

所以,我补充道:

$stmt7 = $pdo->prepare("INSERT INTO tb_aukciono_istorija (user_id, 
    aukciono_id, user, date, bid) SELECT user_id, aukciono_id, user, date, bid 
    FROM tb_auction_bids");

而不是:

while ($r1 = $stmt1_while->fetch(PDO::FETCH_ASSOC)) {
    $stmt7->execute(array(
        ':user_id' => $r1['user_id'],
        ':aukciono_id' => $r3['id'],
        ':user' => $r1['user'],
        ':date' => $r1['date'],
        ':bid' => $r1['bid']
    ));
}

使用这个:

$stmt7->execute();

只有一行代码。酷:)