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.
Epiales
Posts: 1119 Joined: Thu Aug 15, 2013 1:38 am
Post
by Epiales » Tue Oct 14, 2014 2:55 am
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);
}
}
Jackolantern
Posts: 10891 Joined: Wed Jul 01, 2009 11:00 pm
Post
by Jackolantern » Tue Oct 14, 2014 3:14 am
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
Epiales
Posts: 1119 Joined: Thu Aug 15, 2013 1:38 am
Post
by Epiales » Tue Oct 14, 2014 3:17 am
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...
Epiales
Posts: 1119 Joined: Thu Aug 15, 2013 1:38 am
Post
by Epiales » Tue Oct 14, 2014 3:41 am
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>';
Sim
Posts: 410 Joined: Sat Dec 26, 2009 5:37 pm
Post
by Sim » Tue Oct 14, 2014 3:49 am
I don't see the query that goes in $row
but maybe:
Epiales
Posts: 1119 Joined: Thu Aug 15, 2013 1:38 am
Post
by Epiales » Tue Oct 14, 2014 3:52 am
Sim wrote: I don't see the query that goes in $row
but maybe:
I"m still not sure what you two are asking. I'll just post the entire page.
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 Tue Oct 14, 2014 4:32 am, edited 1 time in total.
Epiales
Posts: 1119 Joined: Thu Aug 15, 2013 1:38 am
Post
by Epiales » Tue Oct 14, 2014 4:16 am
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)) {
I really have been spending too much time working on this LOL! Enough to drive ya mad I guess
Thanks guys!
Sim
Posts: 410 Joined: Sat Dec 26, 2009 5:37 pm
Post
by Sim » Tue Oct 14, 2014 4:42 am
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)) {
I really have been spending too much time working on this LOL! Enough to drive ya mad I guess
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..