Not Recognizing Rows

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.

Not Recognizing Rows

Postby Epiales » Mon Oct 13, 2014 9:55 pm

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);
     }
}
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Not Recognizing Rows

Postby Jackolantern » Mon Oct 13, 2014 10:14 pm

The IF statements make it hard for me to see where you are retrieving the $row from. Where is it coming from?
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10893
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Not Recognizing Rows

Postby Epiales » Mon Oct 13, 2014 10:17 pm

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...
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Not Recognizing Rows

Postby Epiales » Mon Oct 13, 2014 10:41 pm

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>'
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Not Recognizing Rows

Postby Sim » Mon Oct 13, 2014 10:49 pm

I don't see the query that goes in $row

but maybe:

Code: Select all
 }if ($row['hpoints'] <1 ) {
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
 
Posts: 412
Joined: Sat Dec 26, 2009 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)

Re: Not Recognizing Rows

Postby Epiales » Mon Oct 13, 2014 10:52 pm

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.
Last edited by Epiales on Mon Oct 13, 2014 11:32 pm, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Not Recognizing Rows

Postby Epiales » Mon Oct 13, 2014 11:16 pm

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!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Not Recognizing Rows

Postby Sim » Mon Oct 13, 2014 11:42 pm

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..
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
 
Posts: 412
Joined: Sat Dec 26, 2009 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x