I have an if statement and am having problems with it.
It needs to say:
If the stamina field is bigger then 0 then + exp field, + gold field, – stamina field else do something else
I have the adding and taking away fields part working. However its not doing it while in this if statement UNLESS I put it in the “do something else” part. However it will not stop at 0 when i put it in that section of the if statement.
<?php
include '../core/init.php';
$id = $_SESSION['user_id'];
$result = mysql_query('SELECT `stamina`FROM `characters` WHERE `id` = "$id"');
$row = mysql_fetch_row($result);
$stam = $row[0];
if ($stam > 0) {
mysql_query('UPDATE `characters` SET `exp` = `exp` + 1, `gold` = `gold`+10, `stamina`=`stamina`-1 WHERE id = "'.$id.'"');
} else {
}
?>
My connection is “required” in the init.php which works completely fine.
I have tried lots of different ways of turning the equation round.
Okay so now I’ve edited the statement and it’s half working now. Here it is:
<?php
include '../core/init.php';
$id = $_SESSION['user_id'];
$stam = mysql_query('SELECT `stamina` FROM `characters` WHERE `id` = "$id"');
if ($stam >= 1){
mysql_query('UPDATE `characters` SET `exp` = `exp` + 1, `gold` = `gold`+10, `stamina`=`stamina`-1 WHERE id = "'.$id.'"');
} else if ($stam < 1){
}
?>
The new problem is that it still runs the query inside the if statement even if $stam smaller than 1.
Perhaps specify that
$stamis aninteger? Try: