2015-12-29 88 views
3

因为我有几个应该包含许多问题的“项目”,我有一个填写视频链接的问题页面,四个答案和四个精灵列表,用户可以设置积分为每个答案。查看最新的ID并添加1

但是,在数据库中我有两个表。 当我填写并执行我的问题页面时,我已经提出了每个答案都有一个id。在表“answer_det”中,Pid,Aid,答案和分数正在设置中。这是它的样子:

enter image description here

的 “问题” 表时,我插入的第一个项目(PID = 1)第一个问题:

enter image description here

我想什么现在要做的就是设置qid(question-id)。我不知道该怎么做,但我认为我应该有一个代码来检查pid的最大数量并添加1,因此同一项目的每个新问题都会得到一个新的qid。如果该pid不在表中,那么qid应该得到值“1”。

因此,如果您查看第一张图片,每个显示行上的qid应为1,因为所有四个答案都属于同一个问题,这是pid = 1的项目的第一个答案。因此,如果我想向同一个项目添加问题,它应该看起来相同,但是使用qid = 2等等。如果我然后为项目2添加一个新的(第一个)问题,那么qid应该从1开始,依此类推。然后,如果我想再次为第一个项目添加一个新问题,代码应该检查最大qid是2,其中pid是1,然后插入一个带有答案但qid = 3的新问题。

它应该在表格“问题”上以相同的方式工作,您可以在第二张图片上看到该问题。当创建第一个问题时,我想让第一个项目(pid = 1的项目)的第一个问题也包含qid = 1和我填充的链接。 pid = 1的项目的第二个问题应该是qid = 2。如果我为新项目添加第一个问题,那么它应该是pid = 2和qid = 1。 这是我现在拥有的代码,它没有任何内容在qid中插入任何两个表中的任何一个。

+3

请仔细阅读[我可以问什么议题有关] (HTTP://计算器。com/help/on-topic) 和[如何问一个好问题](http://stackoverflow.com/help/how-to-ask) 和[完美的问题](http://codeblog.jonskeet .uk/2010/08/29/writing-the-perfect-question /) SO是**不是免费的编码服务** – RiggsFolly

+0

如果您可以发布一些代码,它将对您有所帮助。 –

+0

如果你没有开始编码..开始你的数据库设计,并尝试使用“数据关系”,如多对多等..如果你开始分享你的代码.. –

回答

0

我想你有一组预定义的项目:

项目1>科学

项目2>数学

项目3>历史.....等等。

我建议你为具有projectID和projectName列的项目创建一个单独的表。

现在在您的问题页面中,将这些项目放在下拉(选择选项)格式中,其中projectID作为选项值,projectName作为下拉选择名称。

现在您可以选择项目名称,question_link的输入字段以及其他选择答案及其权重的选项。现在,当你插入这种形式(这个问题)试试编码以下逻辑(我只是尝试在伪代码的方式来写)

//first check if this project already has questions in the question table. 
 
So do something like 
 

 
SELECT* FROM question WHERE pid = $post['projectID'] ORDER BY qid DESC 
 
// or you could do SELECT TOP 1 
 

 
// CASE1: if this project never had any questions, the select would return null. 
 
//Hence this is a first question for this project. Then simply write an insert query that 
 
//would insert $post['projectID'] to pid and 1 in qid (since this is the first question). 
 
//Also, insert a series of rows in the answer_det table that will have $post['projectID'] 
 
//to pid and 1 for qid and the answer and points as submitted. 
 

 

 
// CASE2: if this project already has 1 or more questions, your select query from above 
 
//will return some rows and if you pick $row[0]['qid'], this would be the highest number 
 
//value for question (qid) as we selected using ORDER BY qid DESC. Now, simply do inserts 
 
//in both the tables like you would do as above but this time, in the qid field in both 
 
//the tables, instead of inserting 1, you would insert qid => $row[0]['qid'] + 1. 
 
//You wouldn't need to know what the qid was, all you need is to get the last qid and add 
 
//1 to it. 
 

 
//Also, you can create a separate page to add the projectName as you feel to add new 
 
//projects and insert this into the project_table and this will begin to show up in the 
 
//select options when you are adding new questions.