我正在尝试生成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)
我自己解决了,你不能更新一个空字段。所以我把它设置为-1。