dsgdf45654 2017-04-23 19:20
浏览 60
已采纳

更新查询无法使用随机数更新布尔行

I am trying to generate a either 0 or 1 and when it is generated update a row with the value. 1 being heads and 0 being tails, like a coin flip. I can't use external libraries for this.

The problem is when it generates a 0 it won't update the row but with a 1 it does, and I've no idea as to why.

<?php
include_once('../library/user.php');
include_once('../config/connect.php');

$sql    = "SELECT * FROM coinroulette ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$rows   = $result->fetch_array(MYSQLI_ASSOC);

$id   = $rows['id'];
$user = $_SESSION["user"]->getUsername();

$beginGame   = new DateTime($rows['time']);
$currentTime = new DateTime(date("Y-m-d H:i:s"));
$diff        = $currentTime->diff($beginGame);
if ($currentTime < $beginGame) {
    echo "Remaining:" . $diff->format('%S') . "<br>";
}
$beginGame->sub(new DateInterval('PT15S'));
//echo $beginGame->format('Y-m-d H:i:s') . "<br>" . $currentTime->format('Y-m-d H:i:s') . "<br>" . $rows['time'] . "<br>";

//echo rand(0,1);
//Check is game is in place. (result == empty) game in place.
//Otherwise its over and a new game is made.
if (empty($rows['result'])) {
    echo "Accepting bets.";
    if ($currentTime >= $beginGame) {
        //Needs to be converted to an INT or else it cant be inserted into datbase. Took me 3 hours to figure this error out. :(
        $gamble =  mt_rand(0,1);
        $sql    = "UPDATE coinroulette SET result = '$gamble' WHERE id = '$id'";
        $result = $conn->query($sql);
    }
} else {
    echo "No longer accepting bets.<br>Result: ";
    if ($rows['result'] == "1") {
        echo "Heads<br>";
    } else {
        echo "Tails<br>";
    }

    $sql     = "SELECT * FROM bets WHERE gameID = '$id' AND user = '$user'";
    $result  = $conn->query($sql);
    $betRows = $result->fetch_array(MYSQLI_ASSOC);
    if ($result->num_rows > 0) {
        echo "InGame<br>";
        if ($rows['result'] == $betRows['guess']) {
          if(empty($betRows['result'])){
          $sql = "UPDATE bets SET result = 1 WHERE user = '$user'";
          $result = $conn->query($sql);

          $winAmount = $betRows['amount'] * 2;
          $sql = "UPDATE users SET points = points + '$winAmount' WHERE username = '$user'";
          $result = $conn->query($sql);
          }
          echo "Winner";
        } else {
          $sql = "UPDATE bets SET result = 0 WHERE user = '$user'";
          $result = $conn->query($sql);
          echo "Loser";
        }
    } else {
        echo "Not<br>";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • duanfen7676 2017-04-23 20:03
    关注

    Solved it myself, you cant update a null field. So I set it as -1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突