Im trying to build an easy textbased game in php to learn php. The problem is how do i UPDATE database table if certain parameters i met. 1 barracks cost 3000 and i want to check if the user got 3000 money or more. Only update then or say you need more money.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpsamples";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = UPDATE tbl_registered_users
SET barracks = IF(money = '>3000', barracks + 1)
WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
UPDATE ... SET barracks = barracks + (money > 3000), money = money - 3000 * (money > 3000) WHERE ...