JoelRocks Posted April 28, 2007 Share Posted April 28, 2007 Hey guys, Having problems with the following code: <?php $con = mysql_connect("localhost","remotepa_joel","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("remotepa_harrypotter", $con); $result = mysql_query("SELECT * FROM tbl_question"); while($row = mysql_fetch_array($result)) { ?> <?echo($row['question']);?> <form action="sqltest.php" method="post"> <input type="radio" name="answer" value="<? echo ($row['option_one']); ?>"><? echo ($row['option_one']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_two']); ?>"><? echo ($row['option_two']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_three']); ?>"><? echo ($row['option_three']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_four']); ?>"><? echo ($row['option_four']); ?> <br /> <input type="submit"> </form> <? $answer=$row["answer"]; } mysql_close($con); //echo($answer); $input=($_POST["answer"]); if('$answer' == '$input') { echo("Answer Correct"); } else { echo("Incorrect Answer"); } echo("<br />"); echo($input); echo("<br />"); echo($answer); ?> Although the two values appear to be equal it keeps giving the echo "answer incorrect". It is hosted here:http://www.remotepanzer.com/Joel/sqltest.php Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/ Share on other sites More sharing options...
ignace Posted April 28, 2007 Share Posted April 28, 2007 // leave out the single quotes around variables if ($answer == $input) // using strcmp() and strcasecmp(); // these functions return 0 when equal, < 0 when one is shorter then the other, > 0 when one is longer then the other // case sensitive if (!strcmp($answer, $input)) // case insensitive if (!strcasecmp($answer, $input)) // check: http://be.php.net/manual/en/function.strcmp.php http://be.php.net/manual/en/function.strcasecmp.php Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240472 Share on other sites More sharing options...
JoelRocks Posted April 28, 2007 Author Share Posted April 28, 2007 Not that.... still doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240473 Share on other sites More sharing options...
JoelRocks Posted April 28, 2007 Author Share Posted April 28, 2007 Have ajusted: Still having problems [code<?php $con = mysql_connect("localhost","remotepa_joel","fanfrog"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("remotepa_harrypotter", $con); $result = mysql_query("SELECT * FROM tbl_question"); while($row = mysql_fetch_array($result)) { ?> <?echo($row['question']);?> <form action="sqltest.php" method="post"> <input type="radio" name="answer" value="<? echo ($row['option_one']); ?>"><? echo ($row['option_one']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_two']); ?>"><? echo ($row['option_two']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_three']); ?>"><? echo ($row['option_three']); ?> <br /> <input type="radio" name="answer" value="<? echo ($row['option_four']); ?>"><? echo ($row['option_four']); ?> <br /> <input type="submit"> </form> <? $answer=$row["answer"]; } mysql_close($con); //echo($answer); $input=($_POST["answer"]); if (strcasecmp($answer, $input) == 0) { echo("Answer Correct"); } else { echo("Incorrect Answer"); } echo("<br />"); echo($input); echo("<br />"); echo($answer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240478 Share on other sites More sharing options...
JoelRocks Posted April 28, 2007 Author Share Posted April 28, 2007 Dunno what i could be i am sure the database element is correct? Any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240496 Share on other sites More sharing options...
ignace Posted April 28, 2007 Share Posted April 28, 2007 well, then i would suggest you use my best friend called debug, and do: echo($answer . " == " . $input); if (strcasecmp($answer, $input) == 0) ... Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240500 Share on other sites More sharing options...
JoelRocks Posted April 28, 2007 Author Share Posted April 28, 2007 This is an exact copy of what is said: Severus Snape == Severus Snape (Yes there is a space after the second entry) Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240502 Share on other sites More sharing options...
JoelRocks Posted April 28, 2007 Author Share Posted April 28, 2007 Cheers for tips, problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/49085-solved-having-slight-problems-with-a-piece-of-code/#findComment-240503 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.