dimanche 23 avril 2017

Update query cant update boolean row with a random number?

basically im trying to generate a number, either 0 or 1 and when its generated update a row with the value. 1 being heads and 0 being tails. Like a coin flip. I cant use external libraries for this either.

The problem is when it generates a 0 it wont update the row but with a 1 it does, and I've no idea as to why. I've been trying to figure this out all day and I cant figure out whats going wrong.

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




Aucun commentaire:

Enregistrer un commentaire