I have a form which users will use over and over again to submit an answer to a question. The form has three fields: username, answer, and questionID (this field is hidden).
Each day users get a new question (pulled from the database using the questionID field). Users are only allowed to answer the question once. I have been able to write the php that allows me to submit the user's answer to the question into the database table, however, I am having difficultly with how I should check whether or not they have submitted an answer to the question.
I thought the best way to do this was to first use a select statement to pull from the database what questions (this is identified by the questionID field) they've already answered. Then I thought I could use an if statement to check if the questionID from the form matched the questionID from the select statement. However, each time I test the form whether the questionID appears in the table or not, I get the same echo statement ('sorry you have already submitted an answer').
Could anyone look at the code below and give me a suggestion regarding what I'm doing wrong?
Thanks ever so much,
Siobhan
<?php
// create short variable names
$username=$HTTP_SESSION_VARS['MM_Username'];
$answer=$HTTP_POST_VARS['answer'];
$questionID=$HTTP_POST_VARS['questionID'];
if (!$answer)
{
echo 'You haven't filled in the form.<br />'
.'Please go back and try again.';
exit;
}
$answer = addslashes($answer);
mysql_select_db('projectydb');
$query = "SELECT questionID FROM tbl_userquestion";
$result = mysql_query($query);
if ($result == $questionID);
{
echo 'sorry you have already submitted an answer';
exit;
}
$userquestionID = mysql_insert_id();
$query = "INSERT INTO tbl_userquestion (userquestionID, questionID, username, useranswer) VALUES
(".$userquestionID.", ".$questionID.", '".$username."', '".$answer."')";
$result = mysql_query($query);
if ($result)
echo ' Thank you '.$_SESSION['MM_Username'].';
else
echo "Error: ".mysql_error();
?>