Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Incorrect Comparison (logical error for sure)

$
0
0

I am temporarily including some extra "testing" code within my program to help me troubleshoot it.  It won't be in the final program.  I'm trying to compare the correct answer in a quiz program ($ans) with the user's answer ($_POST['guess']).  Even when the user picks the right answer, the program still spits out, "wrong!"  I am not seeing my logical error here, but I obviously have one.  I attached a screen shot of the output.  I hope it helps.  Thank you all.

if (isset($_POST['qid'])){ //qid is the question id
     $_SESSION['qid']=$_POST['qid'];
    print_r($_SESSION);

$ans = $db->query("SELECT answer FROM QuestionsAnswers WHERE questionid = " .  $_SESSION['qid']);

    echo "<br />";
    print_r ($ans);  //correct answer

while($row = $ans->fetch(PDO::FETCH_ASSOC)) {
    echo "<p>Here is my Thursday 6pm attempt:xxx" . intval($row['answer']) . "xxx</p>";
    echo "<p>User guessed:xxx" . intval($_POST['guess']) . "xxx</p>";
    echo '<p>the extra exes ("xxx") are to demonstrate there are no spaces in the field data.</p>';
}

if (intval($row['answer']) == intval($_POST['guess'])){ //values converted to integers just in case one or both is/are a string
    echo "<p>Right!</p>";}
  else{
    echo "<p>wrong!</p>";
}

It is bugging me that PHPFreaks won't let me log into my regular account.  Cool.  At least I can log in using Facebook.  Different account, same person.  It is still me.  Thanks to all for any help with this one.
 

(please see attached output.)

 

Attached Thumbnails

  • 20131121-1842-screenshot.png

Viewing all articles
Browse latest Browse all 13200

Trending Articles