2016-11-04 51 views
0

我有插入動態考試到mysql數據庫的問題。 我不知道如何在Word中解釋所以這我的示例代碼:插入每個學生的動態考試結果

我有表的考試,這是我的考試表結構:

考試表

+----+-----------+ 
| id | exam_name | 
+----+-----------+ 
| 1 | run test | 
| 2 | swim test | 
+----+-----------+ 

學生表:

+----+-------------+ 
| id | student | 
+----+-------------+ 
| 1 | student one | 
| 2 | student two | 
+----+-------------+ 

問題是如何插入表考試結果,例如學生一次通過考試運行測試但未通過游泳測試。所以表的考試結果將顯示如下:

+---------+------------+--------+ 
| id_exam | id_student | status | 
+---------+------------+--------+ 
|  1 |   1 |  1 | 
|  2 |   1 |  0 | 
+---------+------------+--------+ 

這是我的每一個單一的考試和學生都抓取到HTML表格給每個學生的結果:

<table class='wp-list-table widefat fixed striped posts'> 
     <tr> 
     <th class="bgth manage-column ss-list-width1">Participan Name</th> 
      <th class="bgth manage-column ss-list-width1">Complited Pool Requirement</th> 
      <th class="bgth manage-column ss-list-width1">Complited Theory Exam</th> 
      <th class="bgth manage-column ss-list-width1">Medical Sertificate Checked</th> 
    <th class="bgth manage-column ss-list-width" colspan="2">Action</th> 
     </tr> 
    <input type='hidden' name='exam_total' value='3'><input type='hidden' name='exam_names' value='[{"id":"1","exam_name":"Complited Pool Requirement"},{"id":"2","exam_name":"Complited Theory Exam"},{"id":"3","exam_name":"Medical Sertificate Checked"}]'><tr><td class='manage-column ss-list-width'>Handri angga riawan 
     <input type='hidden' value='1' name='x-id[]'></td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Complited_Pool_Requirement[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='1' > 
      </td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Complited_Theory_Exam[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='2' > 
      </td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Medical_Sertificate_Checked[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='3' > 
      </td><td class='manage-column ss-list-width'><a href='#'>Delete Participan</a></td></tr><td class='manage-column ss-list-width'>Suyadman 
     <input type='hidden' value='2' name='x-id[]'></td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Complited_Pool_Requirement[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='1' > 
      </td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Complited_Theory_Exam[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='2' > 
      </td><td class='manage-column ss-list-width'> 
      <input type='checkbox' name='Medical_Sertificate_Checked[]' value='1'>check if pass 
      <input type='hidden' name='id_exam[]' value='3' > 
      </td><td class='manage-column ss-list-width'><a href='#'>Delete Participan</a></td></tr></table><p><input type="submit" id="btn" name="p-submitted" value="Save Result"></p></form> 

如何獲得學生證所有考試他/她通過並且不能像我在下面展示的那樣插入mysql表格(考試結果)

回答

0

你的方法現在是正確的。因爲這就像

| id_exam | id_student | status | 
 
+---------+------------+--------+ 
 
|  1 |   1 |  1 | 
 
|  2 |   1 |  0 | 
 
+---------+------------+--------+

你可以只添加一個列結果(如分數)的數據類型爲浮動SOT,它將使你還插入十進制值。

學生ID和考試ID爲保存結果的訪問,那麼就在value屬性複選框增值「檢查合格」像

<input type="checkbox" value="exam_id#student_id"/> 

上後採取的行動#分開這個值,你會獲得考試編號和學生證。


爲顯示你的單選按鈕而不是複選框,這樣

Select exam reslut 
<input type="radio" name="swim_exam[]" value="exam_id#student_id#1"/> Pass 
<input type="radio" name="swim_exam[]" value="exam_id#student_id#0"/> Fail 

if you select pass you will get status 1 other wise get 0 with commonly exam and student id also. 

所以它應該像下面的代碼...

<?php 
if(!empty($_POST['swim_exam'])){ 
     foreach($_POST['swim_exam'] as $swim_exam){ 
      $idnStatus = explode("#",$swim_exam); 
      $examId = $idnStatus[0]; 
      $studentId = $idnStatus[1]; 
      $status = $idnStatus[2]; 

      // Your Insert query will perform here.... with all above id. 
     } 
    } 
?> 

嘗試。

+0

問題是如何獲取每個id_exam和id_student的狀態插入到表中,請查閱我的html代碼片段。 :) 無論如何感謝評論 – NinjaCode

+0

你的意思是說,「你的保存結果按鈕上沒有得到考試ID和學生ID」...? – satyawan

+0

我想id_student得到id_exam並獲取狀態(如果chacked值將1,如果沒有值爲0) 問題(見考試結果表)如何讓所有學生的考試狀態,插入到表中。請看示例 謝謝 – NinjaCode