检查最新ID并添加1 [英] Check latest id and add 1

查看:151
本文介绍了检查最新ID并添加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我有几个项目应该包含很多问题,我有一个问题页面,我填写一个videolink,四个答案和四个drow-down列表,用户可以为每个答案设置点。



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





当我插入第一个项目的第一个问题(Pid = 1)时,问题表:





我现在要做的还是设置qid(question-id)。我不知道该怎么做,但我认为我应该有一个代码检查pid的最大qid并添加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中插入。

解决方案

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



项目1>科学



项目2 >数学



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



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



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



现在,您有一个项目名称的选择选项,question_link的输入字段和其他选择的选项,以及他们的体重。现在当你插入这个表单(这个问题)尝试编码下面的逻辑(我将尝试以伪代码的方式写)



  /首先检查这个项目在问题表中是否有问题。所以做一些像SESE * FROM的问题WHERE pid = $ post ['projectID'] ORDER BY qid DESC //或者你可以做SELECT TOP 1 // CASE1:如果这个项目没有任何问题,select将返回null。 //因此这是这个项目的第一个问题。然后只需编写一个插入查询,//将$ post ['projectID']插入到qid中,然后在qid中插入1(因为这是第一个问题)。 //另外,在answer_det表中插入一行,其中$ post ['projectID'] //到pid,1表示qid,答案和点已提交// CASE2:如果该项目已经有1个或更多问题,您从上面选择查询//将返回一些行,如果您选择$ row [0] ['qid'],这将是我们使用ORDER BY qid选择的问题(qid)的最高数值//值DESC。现在,只需在两个表中插入//,就像上面那样做,但是这一次,在这两个表中的qid字段中,而不是插入1,您将插入qid => $ row [0] ['qid'] + 1 //你不需要知道qid是什么,你需要的只是得到最后一个qid并添加// 1 //也可以创建一个单独的页面来添加projectName,因为你觉得添加新的//项目并将其插入到project_table中,当你添加新的问题时,它将开始显示在//选择选项中。 


Since I got several "projects" that should contain many questions each, I have a question-page where I fill a videolink, four answers and four drow-down lists where the user is able to set points for every answer.

However, in the database I have two tables. When I fill and execute my question page I have made so every answer gets an id. In the table "answer_det", Pid, Aid, answer and points is being set. This is how it looks like:

The "question" table when I insert the first question for the first project(Pid=1) :

What I want to do now is to also set the qid(question-id). I'm not sure how to do it, but I think that I should have a code that checks the maximum qid of the pid and add 1 to it so every new question for the same project get a new qid. If the pid isn't in the table, then the qid should get the value "1".

So if you look at the first picture, the qid should be 1 on every showed row since all the four answers belongs to the same question, which is the first one for the project with pid=1. So if I would like to add a question to the same project, it should look the same but with the qid=2 and so on. If I then add a new(first) question for the project 2, the qid should begin on 1 and so on. Then, if i would like to add a new question again for the first project, the code should check that the maximum qid is 2 where pid is 1, and then insert a new question with answers but with qid=3.

It should work the same way on the table "question", which you see on the second picture. When the first question is created, I want the first question for the first project(the one with pid=1) to also have qid=1 and the link that I filled. The second question for the project with pid=1 should then get qid=2. If i add a first question for a new project, it should then be pid=2 and qid=1. This is the code that I have now, and nothing of it inserts anything in the qid in neither of the two tables.

解决方案

I suppose you have a predefined set of projects:

Project 1> Science

Project 2> Maths

Project 3> History.....and so on.

I suggest you create a separate table for projects that would have projectID and projectName columns.

Now in your question-page, have these projects in a dropdown(select option) format with projectID as the option value and projectName as the dropdown select name.

Now you have a select option for project name, an input field for question_link and other select options for answers and their weight-age. Now when you insert this form (this question) try coding the following logic (I will just try to write in pseudo-code manner)

//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.

这篇关于检查最新ID并添加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆