2017-04-23 45 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

什麼類型的結果是'結果'? –

+0

@LucasKrupinski tinyint(1)[boolean] – Shane

回答

0

解決它自己,你不能更新空字段。所以我將它設置爲-1。