【问题标题】:Insert dynamical exam result of each student插入每个学生的动态考试结果
【发布时间】:2016-11-04 08:19:41
【问题描述】:

我在将动态检查插入 mysql 数据库时遇到问题。 我不知道如何用文字解释,所以这是我的代码示例:

我有表格考试这是我的考试表格结构:

考试表

+----+-----------+
| 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>

如何获取学生 ID 和他/她通过的所有考试,但未能插入 mysql 表,如下所示(考试结果)

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    您的方法现在是正确的。像这样

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

    您可以为结果(如 score)数据类型再添加一列作为浮点数,这样您也可以插入十进制值。

    用于访问学生ID和考试ID以保存结果,然后只需在值属性中的复选框“检查是否通过”中添加值,就像

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

    在发布操作时,将此值用 # 分隔,您将获得考试 ID 和学生 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.
            }
        }
    ?>
    

    试试这个。

    【讨论】:

    • 问题是如何获取每个 id_exam 和 id_student 的状态以插入到表中,请查看我的 html sn-p。 :) 无论如何感谢您的评论
    • 您的意思是说,“您无法在保存结果按钮上获取考试 ID 和学生 ID”...?
    • 我想获取 id_student 获取 id_exam 并获取状态(如果被破解的值为 1,否则值为 0)问题是(参见考试结果表)如何获取所有学生考试的状态插入表中。请看示例谢谢
    • 我仍然不明白如何插入多行,因为一个学生和考试有多个状态。所以我很困惑如何获取它们并使用 php 获取它们,例如:学生有这样的考试(检查):运行(检查),游泳(检查)它应该有这样的 sql 查询对吗? : 插入表exam_result 值(1, 1,1), (1,2,0) 我的意思是谢谢
    • 我明白你的意思,我将编辑我的答案,将在该答案下方添加代码,对你有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2021-02-17
    相关资源
    最近更新 更多