0

I am trying to make a textbased game, but I can't seem to figure out of why this code down is not working.

  • It has a mysqli connection in the core file.
  • It has values for the chances in the database.
  • Rank is set in the database.
  • I don't get any errors, only the "Success" and "Failure" messages not showing up.

Code:

<?php

include_once "core.php";

function checkRandom($chance){
return rand(1, 100) <= (int)$chance;
}

$userid = '1';
$getAllQuery = $data->query("SELECT * FROM players WHERE id = '$userid'") or die($data->error);
while ($getall = $getAllQuery->fetch_assoc()) {
$rank = $getall['rank'];
$chance1 = $getall['crime_chance1'];
$chance2 = $getall['crime_chance2'];
$chance3 = $getall['crime_chance3'];
$chance4 = $getall['crime_chance4'];
$chance5 = $getall['crime_chance5'];
$chance6 = $getall['crime_chance6'];
}

if (isset($_POST['crime'])) {

$choice = $_POST['crime'];
$pass = 0;
$fail = 0;

if ($choice == 1 && $rank <= 1) {
  echo "3esd";
  if (checkRandom($chance1)) {
    echo "Success";
  } else {
    echo "Failure";
  }
} else {
  if ($choice == 2 && $rank <= 2) {
    if (checkRandom($chance2)) {
      echo "Success";
    } else {
      echo "Failure";
    }
  }
}

}
echo "<form method='POST' action='#'>";
if ($rank >= 1) {
echo "<label><input type='radio' name='crime' value='1'>Crime 1 " . $chance1 . "% chance</label><br />";
if ($rank >= 2) {
echo "<label><input type='radio' name='crime' value='2'>Crime 2 " . $chance2 . "% chance</label><br />";
if ($rank >= 3) {
echo "<label><input type='radio' name='crime' value='3'>Crime 3 100% chance</label><br />";
}
}
}
echo "<input type='submit'>
</form>";
?>

I would really appreciate some help. :D

Dave
  • 991
  • 1
  • 7
  • 15
  • Check the case sensitivity on your SQL return. Sometimes it returns things in default upper case ['RANK'] vs ['rank']. Also, you might be using MySQLi, but you're still leaving yourself open to database injection attacks. You need to make $userid a bound variable. – Mark Jun 11 '14 at 19:18
  • @Mark I usually use a function for real escape around stuff like that, but I haven't got that far yet. – user3731477 Jun 11 '14 at 19:19
  • http://stackoverflow.com/questions/732561/why-is-using-a-mysql-prepared-statement-more-secure-than-using-the-common-escape – Mark Jun 11 '14 at 19:20
  • @Mark There are no spelling mistakes. – user3731477 Jun 11 '14 at 19:28
  • Case sensitivity, not spelling. What rank are you testing with? The code above would never generate anything for Rank 3+ – Mark Jun 11 '14 at 19:29
  • I have been testing with rank 1 and 2, and even with "if (1 == 1) { code }". Everything is okay with the names, so I don't know what is wrong. – user3731477 Jun 11 '14 at 19:30
  • Well the code as written works fine for me except in a few cases. – Mark Jun 11 '14 at 19:31
  • I added an answer with my notes about what doesn't work. – Mark Jun 11 '14 at 19:35

2 Answers2

0

This code seems to work fine except under the following cases:

1) Nothing is handling Rank 3+ as the IF statements are <= 1 or <= 2 and 3 is > then 1 and 3.

2) If you select the first option, and you are not rank 1, the code won't return anything, because the first IF statement is limited to the selected choice being 1 AND the Rank being <= 1.

3) If you select the second option, and you are not rank 2, the code won't return anything, because the second IF statement is limited to the selected choice being 1 AND the Rank being <= 2.

Mark
  • 861
  • 9
  • 17
  • The problem was my rank. It was 3 in the database, and as you mentioned the IF statements were not right. Thanks a lot. I really have to watch out for my dizzyness. – user3731477 Jun 11 '14 at 19:36
  • 1
    I edited your(OP) if statement to indent them in a cascaded manner... this can help clarify your code and determine if you are missing any else conditions. – Dave Jun 11 '14 at 19:37
  • Dave's addition was a good one, and will help with showing where you are falling in your IF loop, so make sure to take a look at his answer/suggestion too. It is always a good idea to set up a default case for complicated if/else switches to make sure you get SOME indication of what your code is doing. You might also look at CASE statements, too. – Mark Jun 11 '14 at 19:39
  • if ($rank >= 1) { if ($choice == 1) { if (checkRandom($chance1)) { echo "Success"; } else { echo "Failure"; } } else if ($choice == 2) { if (checkRandom($chance2)) { echo "Success"; } else { echo "Failure"; } } else if ($choice == 3) { if (checkRandom($chance2)) { echo "Success"; } else { echo "Failure"; } } } else if ($rank >= 2) { if ($choice == 2) { if (checkRandom($chance2)) { echo "Success"; } else { echo "Failure"; } } } elseif ($rank >= 3) { if ($choice == 3) { if (checkRandom($chance3)) { echo "Success"; } else { echo "Failure"; } } } – user3731477 Jun 11 '14 at 20:03
0

see the following addition }ELSE{ # condition not handled ECHO "WE ARE HERE";

if ($choice == 1 && $rank <= 1) {
  echo "3esd";
  if (checkRandom($chance1)) {
    echo "Success";
  } else {
    echo "Failure";
  }
} else {
  if ($choice == 2 && $rank <= 2) {
    if (checkRandom($chance2)) {
      echo "Success";
    } else {
      echo "Failure";
    }
  }ELSE{   # condition not handled
    ECHO "WE ARE HERE";  
  }
}
Dave
  • 991
  • 1
  • 7
  • 15