根据用户输入显示数据库中的数据
I'm creating a way that a "teacher" could make an exam and the "student" can take an exam.
First off, it is also possible for a teacher to make new questions, using this query:
INSERT INTO questions (question, type) VALUES ('$question', '$type')
In the database, I set questions to also have question_id which is auto incremented after each entry. Then on a separate page, they can pick which questions they would like to add to the exam. So I just:
SELECT * FROM questions
Then there is a checkbox for them to check which questions to add the use this query:
INSERT INTO exams (question_id) VALUES ('$question_id')
The table exams also has an auto incremented exam_id. So now I would like to display the questions the teacher picked, but I don't even know what type I should store question_id in exams (right now it is INT) so I can loop through them.
ie. Teacher picks questions 1,2,4,10 and query for getting the question would look like
SELECT question FROM questions WHERE question_id='1,2,4,10'
我正在创建一种“老师”可以参加考试的方式,“学生”可以参加考试 。 p>
首先,教师也可以使用此查询提出新问题: p>
插入问题(问题) ,类型)VALUES('$ question','$ type')
code> pre>
在数据库中,我设置的问题也有question_id,每次输入后自动递增 。 然后在单独的页面上,他们可以选择他们想要添加到考试中的问题。 所以我只是: p>
SELECT * FROM questions
code> pre>
然后有一个复选框供他们检查哪些问题 添加使用此查询: p>
INSERT INTO考试(question_id)VALUES('$ question_id')
code> pre>
表格考试也有一个自动递增的exam_id。
现在我想显示老师选择的问题,但我甚至不知道我应该在考试中存储question_id的类型(现在它是INT)所以我可以 循环遍历它们。 p>
ie。 老师选择问题1,2,4,10并查询问题看起来像 p>
SELECT question FROM questions WHERE question_id ='1,2,4,10'\ n code> pre>
div>
Assuming you are getting the question id by POST or GET, Try this:
$selected = implode(',', $_REQUEST['selectedquestionids']);
SELECT question FROM questions
WHERE question_id IN ($selected)
GROUP BY question_id;
Hope this may help.