Jump to content

[SOLVED] Having slight problems with a piece of code


JoelRocks

Recommended Posts

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

 

// 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

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.