Page 1 of 1

Not Recognizing Rows

Posted: Tue Oct 14, 2014 2:55 am
by Epiales
Okay, everything is working great up until the end when it it to check the row hpoints in the database. It should check to see if the row is equal or less than 0, but it's completely skipping that part. I don't see where I have written anything out of order, but maybe someone here can. I need to know why it skips checking the health and why it doesn't echo out that they have died... The database will go negative in the row hpoints, but it's not recognizing it in the php.

Code: Select all

 if (isset($_POST['dropdown'])) {
        // cast to integer to avoid malicious values
        $dropdown = (int)$_POST['dropdown'];
        
        $rand_num = rand(100,200);
$atk_player = ($rand_num /100) * $_POST['dropdown'] - $attack_user['defense'];
$atk_player2 = number_format($atk_player);
        
        if($_POST['dropdown'] < 1 || $_POST['dropdown'] > 20){
            echo '<span id="errormess"><big><center><font color="red"><b>Only between 1 and 20</b></font></span></big><br><br>';
            exit;
    
        }elseif($_POST['dropdown'] > $row['bullets']){
            echo '<br><span id="errormess"><big><center><font color="red"><b>You do not have enough Bullets!</b></font></span></big><br><br>';

            exit;
        }elseif($id == $_SESSION['userid']){
            echo '<br><span id="errormess"><big><center><font color="red"><b>You cannot attack yourself!</b></font></span></big><br><br>';
            exit;        
        }elseif ($row['bullets'] <= 0) {
            echo '<span id="errormess"><big><center><font color="red"><b>You do not have any bullets!</b></font></span></big><br><br>';
            exit;            
        }elseif ($row['bullets'] < $_POST['dropdown']) {
            echo '<span id="errormess"><big><center><font color="red"><b>You do not have enough bullets!</b></font></span></big><br><br>';
            exit;
        }if ($atk_player2 > 0) {

    $num = 0 + $atk_player2;
    
      $outputList .= '<b>Your battle has begun! </b>';
      $outputList1 .= '<b>You have won the battle! </b>';
      $outputList2 .= '<b>You have taken ' . $num . ' health from ' .$attack_user['username']. '</b>';
      $outputList3 .= '<b>' .$_POST['dropdown']. ' bullets have been removed from your inventory! </b>';
      $outputList5 .= '<b>Your battle has ended! </b>';
        
$updatehpoints = "UPDATE users SET hpoints=hpoints - $atk_player2 WHERE `id`='".$id."'";
$query = mysqli_query($db_conx, $updatehpoints);

$updatebullets = "UPDATE users SET bullets=bullets - $_POST[dropdown] WHERE id = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $updatebullets);
        
        }else{
        
    $num = 0 - $atk_player2;
      $outputList .= '<b>Your battle has begun! </b>';        
      $outputList1 .= '<b>You lost the battle! </b>';
      $outputList2 .= '<b>You have lost ' . $atk_player2 . ' health ' . '</b>';
      $outputList3 .= '<b>You gave ' . $num . ' health to  ' .$attack_user['username']. '</b>';
      $outputList4 .= '<b>' .$_POST['dropdown']. ' bullets have been removed from your inventory! </b>';

$updatehpoints = "UPDATE users SET hpoints=hpoints - $atk_player2 WHERE `id`='".$id."'";
$query = mysqli_query($db_conx, $updatehpoints);

$updatebullets = "UPDATE users SET bullets=bullets - $_POST[dropdown] WHERE id = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $updatebullets);

        }if ($row['hpoints'] <= 0) {
    
      $outputList6 .= '<b>Your have died! </b>';
    
        }else{
        
      $outputList5 .= '<b>Your battle has ended! </b>';
    
$updatehpoints = "UPDATE users SET hpoints=hpoints + $atk_player2 WHERE id = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $updatehpoints);
     }
}

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 3:14 am
by Jackolantern
The IF statements make it hard for me to see where you are retrieving the $row from. Where is it coming from?

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 3:17 am
by Epiales
Jackolantern wrote:The IF statements make it hard for me to see where you are retrieving the $row from. Where is it coming from?
Not sure what u're asking....this is the code in question

Code: Select all

       }if ($row['hpoints'] <= 0) {    
      $outputList6 .= '<b>Your have died! </b>';
        }else{    
      $outputList5 .= '<b>Your battle has ended! </b>';
It's supposed to check the row hpoints to see if it went below 0 or not, but it skips this and automatically moves to the else...

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 3:41 am
by Epiales
I looked closer and thought it was because I wasn't updating the database before it checked the row, so it would still show it being positive. But I changed it to update the database and it still doesn't work:

Code: Select all

$updatehpoints = "UPDATE users SET hpoints=hpoints - $atk_player2 WHERE `id`='".$id."'";
$query = mysqli_query($db_conx, $updatehpoints);

$updatebullets = "UPDATE users SET bullets=bullets - $_POST[dropdown] WHERE id = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $updatebullets);

$updatehpoints = "UPDATE users SET hpoints=hpoints + $atk_player2 WHERE id = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $updatehpoints);

        }if ($row['hpoints'] <= 0) {
    
      $outputList6 .= '<b>Your have died! </b>';
    
        }else{
        
      $outputList5 .= '<b>Your battle has ended! </b>'; 

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 3:49 am
by Sim
I don't see the query that goes in $row

but maybe:

Code: Select all

 }if ($row['hpoints'] <1 ) {

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 3:52 am
by Epiales
Sim wrote:I don't see the query that goes in $row

but maybe:

Code: Select all

 }if ($row['hpoints'] <1 ) {
I"m still not sure what you two are asking. I'll just post the entire page.

Code: Select all

REMOVED, AS WAS NOT NEEDED
I should be able to use the $row or use the $user_health based on the function I have in my functions that is included... but neither one work.

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 4:16 am
by Epiales
Okay, I must be either stupid or just tired... probably both as of this moment. I never added:

Code: Select all

$sql = "SELECT * FROM users WHERE id = '$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
:oops: :oops: :oops: :oops: :oops:

I really have been spending too much time working on this LOL! Enough to drive ya mad I guess :D Thanks guys!

Re: Not Recognizing Rows

Posted: Tue Oct 14, 2014 4:42 am
by Sim
Epiales wrote:Okay, I must be either stupid or just tired... probably both as of this moment. I never added:

Code: Select all

$sql = "SELECT * FROM users WHERE id = '$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
:oops: :oops: :oops: :oops: :oops:

I really have been spending too much time working on this LOL! Enough to drive ya mad I guess :D Thanks guys!

I believe

Code: Select all

$row = mysqli_fetch_array($user_query, MYSQLI_ASSOC);
would be better then this:

Code: Select all

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
when just fetching 1 record..