[英]How can I filter out questions that have been answered by a user?
I am building a small quiz/test engine with AJAX, PHP, and MySQL. 我正在使用AJAX,PHP和MySQL构建小型测验/测试引擎。 All of the data (questions, answers, etc.) is stored in a database.
所有数据(问题,答案等)都存储在数据库中。
The issue that I am having is that I can't find out how to filter out the questions that have been answered by the user that is logged in, therefore resulting in questions being repeated and a never ending quiz/test. 我遇到的问题是我无法找出如何过滤已登录用户回答的问题,因此导致问题重复出现且永无休止的测验/测试。
When you click the "next question" button, an AJAX request is sent to submit_answer.php
which submits the answer (adds row to user_answers
table) and it also sends a request to get_question.php
which then returns JSON of the question information (question, answers, etc.) but I can't seem to get it to not select questions that have been answered (questions in the user_answers
table). 当您单击“下一个问题”按钮时,AJAX请求将发送到
submit_answer.php
,后者提交答案(将行添加到user_answers
表中),并且还将请求发送到get_question.php
,然后该请求返回问题信息的JSON(问题,答案等),但我似乎无法选择不回答的问题( user_answers
表中的问题)。 Here is what I've got right now: 这是我现在得到的:
$question = mysql_query("SELECT *, q.id qid, q.question question_text
FROM questions q, user_answers ua
WHERE q.id != ua.question_id
AND ua.test_id = $test
AND ua.user_id = $_SESSION[userid]
ORDER BY rand()
LIMIT 1");
$q = mysql_fetch_assoc($question);
This query is supposed to select the questions that have the test_id
of the current test, has the user_id
as the current user, and not in the user_answers
table. 该查询应该选择具有当前测试的
test_id
,具有user_id
作为当前用户的问题,而不是位于user_answers
表中的问题。 But apparently, I'm just not doing it right. 但显然,我只是做得不好。
You can see the entire get_question.php
file here: http://pastebin.com/Td6mqp49 您可以在此处查看整个
get_question.php
文件: http : get_question.php
Here are my table structures as requested by @MadaraUchiha: 这是@MadaraUchiha要求的我的表结构:
questions
user_answers
SELECT *, q.id qid, q.question question_text
FROM questions q
WHERE NOT EXISTS (
SELECT TRUE
FROM user_answers ua
WHERE ua.question_id = q.id
AND ua.test_id = $test
AND ua.user_id = $_SESSION[userid]
)
ORDER BY rand()
LIMIT 1
The reason why your query wasn't working is that q.id != ua.question_id
will match as long as there is at least one answer that doesn't belong to the question - any answer. 您的查询无法正常运行的原因是,只要至少有一个不属于该问题的答案- 任何答案,
q.id != ua.question_id
就会匹配。
You need a left outer join between the questions to the user_answers tables. 您需要在问题与user_answers表之间建立左外部联接。
The final query looks like: 最终查询如下:
SELECT *, q.id qid, q.question question_text
FROM questions q left outer join
user_answers ua
on q.id = ua.question_id and
ua.test_id = $test and
ua.user_id = $_SESSION[userid]
WHERE ua.user_id is null
ORDER BY rand()
LIMIT 1
SELECT * FROM
questions q
WHERE
q.id NOT IN (
SELECT question_id as id
FROM user_answers
WHERE ua.test_id = $test AND ua.user_id = $_SESSION[userid]
)
On a side note, make sure you properly sanitize the data before passing to the SQL. 附带说明一下,在传递给SQL之前,请确保已正确清理数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.