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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.