I am currently building an multiple choice quiz module for my website. There is a list with 20 questions, which can only be answered by customers that are logged in.
I have three tables:
table questions, with fields questions.id, questions.description
table options, with fields options.id, options.questionid, options.trueorfalse
table answers, with fields answers.id, answers.customerid, answers.questionid, answers.chosenoption
The questions come at random, rand(), by an ajax-call, so only the questions that are not answered by a customer are being showed. Once all questions are answered, the final score is echoed.
Can someone help me with the correct sql statement the php script should run to show up the questions that have NOT been answered by a customer?
Currently it seems to work but I sometimes have to submit twice the same answer. Actually the first time it is aleady being sent, but the ajax call shows up the same question.
This is the sql statement I use...
SELECT * from questions
where not exists (
select answers.questionid from answers
where answers.questionid = questions.id
and answers.customerid = '".$customerid."' )
order by rand()
limit 1"
The data is being sent by a JQuery post (works well). Inside the page quiz_ajax.php
<script type="text/javascript">
$(document).ready(function(){
$("#ajax-post-answer").submit(function(){
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "answerpost.php",
data: str
});
callquiz();
return false;
});
});
</script>
Inside the page quiz.php (main page)
<script type="text/javascript">
function callquiz()
{
var newDate = new Date;
$.ajax({
method: 'POST',
url : 'quiz_ajax.php?' + newDate.getTime(),
dataType : 'text',
success: function (text) {
$('#updateMequiz').html(text); }
});
}
callquiz();
</script>
<div id="updateMequiz"></div>
So the function callquiz, should refresh this div with a new question. I am investigating this problem for 24 hours now, so I am very curious to hear what I am doing wrong.
Thanks in advance!!