elrobbo6 Posted May 4, 2011 Share Posted May 4, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/ Share on other sites More sharing options...
Pikachu2000 Posted May 4, 2011 Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210199 Share on other sites More sharing options...
Psycho Posted May 4, 2011 Share Posted May 4, 2011 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. Or, just use strcasecmp() Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210232 Share on other sites More sharing options...
Pikachu2000 Posted May 4, 2011 Share Posted May 4, 2011 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. Or, just use strcasecmp() Yeah, or that . . . I keep forgetting that exists for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210237 Share on other sites More sharing options...
elrobbo6 Posted May 4, 2011 Author Share Posted May 4, 2011 the spaces were there because it was just the last thing i was trying. Any way you could show a sample code using what you were talking about? Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210267 Share on other sites More sharing options...
cunoodle2 Posted May 4, 2011 Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210270 Share on other sites More sharing options...
elrobbo6 Posted May 4, 2011 Author Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210271 Share on other sites More sharing options...
elrobbo6 Posted May 4, 2011 Author Share Posted May 4, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210274 Share on other sites More sharing options...
cunoodle2 Posted May 4, 2011 Share Posted May 4, 2011 very nicely done!! Congrats Quote Link to comment https://forums.phpfreaks.com/topic/235476-trouble-with-variables-in-ifelse/#findComment-1210391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.