He compared the strings $wrong and $right in his reply.
Well, your logic seems jarbled. Let's say this:
-- Question: How much would could a wood chuck chuck, if a wood chuck could chuck wood?
-- Try: A would chuck could chuck as much chuck as a would chuck could chuck wood.
----> We run that through validation, that's wrong, add it to our table of tries
-- Try: A wood chuck could chuck as much would as a would chuck could chuck wood.
----> We run that through validation, that's wrong, add it to our table of tries
-- Try: A wood chuck could chuck as much wood as a wood chuck could chuck wood.
----> We run that through validation, that's close, but still wrong, add it to our table of tries
-- Try: If a wood chuck could chuck wood, a wood chuck could chuck as much wood as a wood chuck could chuck wood.
----> We run that through validation, it's correct, so we count the number of tries, and then let them move on (guessing that's what you want them to do)
Well, the code for this would be:
<?php
// For each question, just add it to the array,
// the array key being the question number
$answers[1] = 'If a wood chuck could chuck wood, a wood chuck could chuck as much wood as a wood chuck could chuck wood.';
function add_try($user, $answer, $question)
{
$user = mysql_real_escape_string($user);
$question = mysql_real_escape_string($question);
$answer = mysql_real_escape_string($answer);
$query = "INSERT INTO `table_name` (user, qID, try) VALUES ('$user', '$question', '$answer')";
$result = mysql_query($query);
if(!$result){ return FALSE; }
else{ return TRUE; }
}
function count_tries($question, $user)
{
$user = mysql_real_escape_string($user);
$question = mysql_real_escape_string($question);
$query = "SELECT COUNT(*) AS tries FROM `table_name` WHERE user='$user' AND qID='$question'";
$result = mysql_query($query);
if(!$result){ return FALSE; }
else{
if(mysql_num_rows($result)>0)
{
list($tries) = mysql_fetch_array($result);
return $tries;
}
else{ return 1; }
}
}
if(isset($_POST['submit']) && $_POST['submit'] != '')
{
// Check the input
if($_POST['answer'] == $answers[$_POST['question']])
{
echo 'Correct!!<br>You answered correctly in '.count_tries($_POST['question'], $_POST['user']).' tries.<br><br>';
echo '<a href="page.php?question='.$_POST['question']+1.'">Next Question</a>';
}
else
{
$add = add_try($_POST['user'], $_POST['answer'], $_POST['question']);
if($add === TRUE)
{
echo 'Sorry, that was incorrect. Please try again!';
// Show them the question again
}
else
{
echo 'We were unable to add your try to the database. Please try again later.<br><br>Sorry, that was incorrect. Please try again!';
// Show them the question again
}
}
}
?>
That might be something more up your ally....
~Brett