2017-04-23 18 views
1

0または1のどちらかを生成しようとしています。頭が1、尾が0で、コインフリップのようです。私はこれのために外部ライブラリを使用することはできません。更新クエリは、ブール値の行を乱数で更新できません。

問題は、0を生成すると行が更新されないが、1が実行されるため、なぜか分かりません。

<?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>"; 
    } 
} 
?> 
+0

どのようなタイプの列が 'result'ですか? –

+0

@LucasKrupinski tinyint(1)[ブール値] – Shane

答えて

0

あなた自身で解決しました。あなたはnullフィールドを更新できません。だから私は-1としてそれを設定します。

関連する問題