Jump to content

trouble with variables in IF/ELSE


elrobbo6

Recommended Posts

Hello everyone,  I am having major issues trying to figure this one out.  So  here is the code....

 

$useranswer= ( $_POST["Text1"]);

 

 

$query  = "SELECT Answer FROM images_guess WHERE id=16";

$result = mysql_query($query);

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

  echo "Answer :{$row["Answer"]} <br>";

echo "User Answer:$useranswer <br>";

       

}

$result2 = $result. " ";

$useranswer2 = $useranswer . " ";

 

if ($result2==$useranswer2)

  echo "right";

else

  echo "wrong";

?>

 

Basically, I am taking whatever was put into "Text1"  and finding out if it matches what was in the Answer row in my table.  The two variables I used, $result and $useranswer are both working because they do print out on the next page.  The problem is the If/else statement is not working with them.  They are both text if that helps.  Any advice would help a ton, i'm at a loss.

Thanks!

Link to comment
Share on other sites

Why are you adding a space to the end of each variable? You should trim() them to remove any extraneous leading or trailing whitespace, then do the comparison. If the answer isn't case-sensitive, you should also make sure both strings are the same case by using either strtoupper or strtolower on them as well.

Link to comment
Share on other sites

Well I can pretty much guarantee you that $result2 and $useranswer2 will NEVER be equal based upon your code.  You have the answer being set to a posted item..

$useranswer= ( $_POST["Text1"]);

 

But you have the result set to the of a mysql query call..

$result = mysql_query($query);

 

So in essence the only way this would be true would be if the user's input was basically a multidimensional array.  I'm not really following what you are trying to do here so if possible provide some examples and we will be able to better help.

 

To learn more about mysql_query() function call see this..

http://php.net/manual/en/function.mysql-query.php

Link to comment
Share on other sites

ah, I thought it might be something like that.  Basically the $useranswer= ( $_POST["Text1"]); is what a user types into a text field on a html page. and the $result = mysql_query($query); is a row called 'Answer' containing text stored in a table from a database.  So your saying I need to make the $result into something else?  because when I use echo "Answer :{$row["Answer"]} <br>";  It does print that information.  So how would I make a variable that is that text it pulls off the table?  And I will check that website. thank you.

Link to comment
Share on other sites

SWEET! Ok, I figured it out so I thought I would post it for anyone else having this problem...

 

$useranswer= ( $_POST['Text1']);

 

$query  = "SELECT Answer FROM images_guess WHERE id=16";

$result = mysql_query($query);

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

$realanswer = $row['Answer']; <-----------------------just turned the actual text pulled in into a variable

echo "Answer :$realanswer <br>";

  echo "User Answer:$useranswer <br>";

}

 

if ($realanswer==$useranswer)

echo "right";

else

echo "wrong";

 

?>

Thanks a bunch you guys!!!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.