2015-12-29 20 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.