Jump to content

[SOLVED] is there improper use of exits; in this code???


Recommended Posts

<?php
$random1 = rand(75,100);
$random2 = rand(20,30);
$random3 = ($random2 * 0.001);

$statgain = ($player->energy * $player->awake / $random1);
$statgain2 = ($_POST['energy_str'] * $player->awake / $random1);
$statgain3 = ($_POST['energy_def'] * $player->awake / $random1);
$statgain4 = ($_POST['energy_spd'] * $player->awake / $random1);
// Awake loss.

$awakeloss = ($statgain * $random3);
$awakeloss2 = ($statgain2 * $random3);
$awakeloss3 = ($statgain3 * $random3);
$awakeloss4 = ($statgain4 * $random3);


if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy)
{
    echo"you dont have enough energy left";
    echo"<br><br><a href=\"gym.php\">Back</a>";
    exit;
}
else if ($_POST['energy_str'])
{
    if($player->energy < 1 || $_POST['energy_str'] > $player->energy)
    {
        echo "<br>You dont have any energy left";
        echo"<br><br><a href=\"gym.php\">Back</a>";
        exit;
    }
    

$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_str'], ($player->awake - $awakeloss2) < 0 ? 0 : $player->awake - $awakeloss2, $player->strength + $statgain2, $player->total + $statgain2, $player->id ));
echo "<br>You trained\n" . $_POST['energy_str'] . "\ntimes and You gained " . number_format($statgain2, 2, ".", ",") . " Strength<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";
exit;
}

if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy)
{
    echo"you dont have enough energy left";
    echo"<br><br><a href=\"gym.php\">Back</a>";
    exit;
}
else if ($_POST['energy_def'])
{
    if($player->energy < 1 || $_POST['energy_def'] > $player->energy)
    {
        echo "<br>You dont have any energy left";
        echo"<br><br><a href=\"gym.php\">Back</a>";
        exit;
    }
    
$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_def'], ($player->awake - $awakeloss3) < 0 ? 0 : $player->awake - $awakeloss3, $player->defense + $statgain3, $player->total + $statgain3, $player->id ));
echo "<br>You trained\n" . $_POST['energy_def'] . "\ntimes and You gained " . number_format($statgain3, 2, ".", ",") . " Defense<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";
exit;
}

if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy)
{
    echo"you dont have enough energy left";
    echo"<br><br><a href=\"gym.php\">Back</a>";
    exit;
}
else if ($_POST['energy_spd'])
{
    if($player->energy < 1 || $_POST['energy_spd'] > $player->energy)
    {
        echo "<br>You dont have any energy left";
        echo"<br><br><a href=\"gym.php\">Back</a>";
        exit;
    }
    
$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_spd'], ($player->awake - $awakeloss4) < 0 ? 0 : $player->awake - $awakeloss4, $player->speed + $statgain4, $player->total + $statgain4, $player->id ));
echo "<br>You trained\n" . $_POST['energy_spd'] . "\ntimes and You gained " . number_format($statgain4, 2, ".", ",") . " Speed<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";
exit;
}
?>
    <form method='post'><br>
    <table width='80%' align='center'>
        
        <tr>
            <td rowspan='3' width='10'>
                 
            </td>
            <td rowspan='3' width='100'>

                 
            </td>
            <td align='center'><b>Strength</b></td>
            <td align='center'><b>Defense</b></td>
            <td align='center'><b>Speed</b></td>
            <td rowspan='3' width='10'>
                 

            </td>
        </tr>

        <tr>
            <td align='center'><input type='text' name='energy_str' value='0' size='5' maxlength='5'></td>
            <td align='center'><input type='text' name='energy_def' value='0' size='5' maxlength='5'></td>
            <td align='center'><input type='text' name='energy_spd' value='0' size='5' maxlength='5'></td>
        </tr>
        <tr>
            <td colspan='7' align='center'><input type='submit' name='train' value='Train'></td>
        </tr>
    </table>
    </form>

 

basically im having a problem, when i put any other values in the defense or speed... it will echo the message.. but it wont run the query.

 

i think i might not needing half the exit; s in this code,

 

the idea is that the user can put a value in each box then hit submit and it will run all 3 queries according to the value.

 

 

well the immeadiate issue is that for some reason the queries for full_defense and full_speed arnt running, yet the echos are.

 

i dont know why or how this can happen.

 

i must be something to do with this part of the script,

 

<?php	
if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy)
{
    echo"you dont have enough energy left";
    echo"<br><br><a href=\"gym.php\">Back</a>";
exit;
    
}

if ($_POST['energy_str'])
{

$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_str'], ($player->awake - $awakeloss2) < 0 ? 0 : $player->awake - $awakeloss2, $player->strength + $statgain2, $player->total + $statgain2, $player->id ));
echo "<br>You trained\n" . $_POST['energy_str'] . "\ntimes and You gained " . number_format($statgain2, 2, ".", ",") . " Strength<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";

}

if ($_POST['energy_def'])
{
    
$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_def'], ($player->awake - $awakeloss3) < 0 ? 0 : $player->awake - $awakeloss3, $player->defense + $statgain3, $player->total + $statgain3, $player->id ));
echo "<br>You trained\n" . $_POST['energy_def'] . "\ntimes and You gained " . number_format($statgain3, 2, ".", ",") . " Defense<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";

}

if ($_POST['energy_spd'])
{
     
$query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $_POST['energy_spd'], ($player->awake - $awakeloss4) < 0 ? 0 : $player->awake - $awakeloss4, $player->speed + $statgain4, $player->total + $statgain4, $player->id ));
echo "<br>You trained\n" . $_POST['energy_spd'] . "\ntimes and You gained " . number_format($statgain4, 2, ".", ",") . " Speed<br>" ;
echo"<br><br><a href=\"gym.php\">Back</a>";

}
?>

exit() is fundamentally the same as die();

 

it's good practice to use exit(); after header() redirects so as to avoid the further execution of code.

 

using exit or die will stop the execution of the script you are running .. so, if the script reaches either command, no further IF/ELSE, etc. statements, or anything for that matter, will execute.

 

you should definitely avoid using exit() in the manner you were/are.  there are much cleaner, practical ways to finish scripts than to just hit it with an exit()/die().

exit() is fundamentally the same as die();

 

it's good practice to use exit(); after header() redirects so as to avoid the further execution of code.

 

using exit or die will stop the execution of the script you are running .. so, if the script reaches either command, no further IF/ELSE, etc. statements, or anything for that matter, will execute.

 

you should definitely avoid using exit() in the manner you were/are.  there are much cleaner, practical ways to finish scripts than to just hit it with an exit()/die().

 

okay thanks for the information...

 

from what ive understood from this thread and reading others... exit; will kill the script even if its called in a if statement which is false...

 

if this is true then my code above shouldnt work with any in... however it does and the exit; doesnt get used.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.