使用mysql和php吸引来宾和学生参加

时间:2019-06-11 22:24:46

标签: php mysql

我正在尝试使用PHPMySQL跟踪学生和来宾的出勤情况。

我有三个表:学生,客人和出勤。

  

学生:student_id | first_name | last_name |成绩

     

客人:guest_id | first_name | last_name |成绩

     

出勤:id |参加| student_id | guest_id

“出勤表”中的“ student_id”和“ guest_id”是分别引用“来宾”和“ students”表中的“ guest_id”和“ student_id”的外键。

网页显示一个表格,其中列出了学生和来宾,每个名称旁边都有一个复选框。提交表单后,$ _ POST数据将包含在场的学生和来宾。

我想根据该人是学生还是客人来填充guest_id或student_id外键字段。当我提交表单时,$_POST数据来自表单

  

$ _ POST ['guest {guest_id}'] = guest_id和   $ _POST ['student {student_id}'] = student_id

     

例如$ _POST ['student11'] = 11和$ _POST ['guest52'] = 52

。我如何获取该帖子数据并确定条目是客人还是学生?

我的表单代码是:

<form class="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <table class="table">
            <tr>
                <th class="col-md-2 col-sm-2 col-xs-2"></th>
                <th class="col-md-8 col-sm-8 col-xs-8">Name</th>
                <th col-md-2 col-sm-2 col-xs-2>Grade</th>
            </tr>
            <tr>
                <td></td><td><strong>---Students---</strong></td><td></td>
            </tr>

            <?php

            if( mysqli_num_rows($studentResult) > 0 ) {
                while( $row = $studentResult->fetch_assoc() ) {
                    echo "<tr>";
                    echo "<td>" . '<input type="checkbox" name="student' . $row['id'] . '"' . 'value="' . $row['id'] . '">';
                    echo "<td>" . $row['first_name'] . " " . $row['last_name'] . "</td><td>" . $row['grade'] . "</td>";
                    echo "</tr>";
                }
            } else { // if no entries
                echo "<div class='alert alert-warning'>You have no students!</div>";
            }

            ?>
            <tr>
                <td></td><td><strong>---Guests---</strong></td><td></td>
            </tr>
            <?php

            if( mysqli_num_rows($guestResult) > 0 ) {
                while( $row = mysqli_fetch_assoc($guestResult) ) {
                    echo "<tr>";
                    echo "<td>" . '<input type="checkbox" name="guest' . $row['id'] . '"' . 'value="' . $row['id'] . '">';
                    echo "<td>" . $row['first_name'] . " " . $row['last_name'] . "</td><td>" . $row['grade'] . "</td>";
                    echo "</tr>";
                }
            }
            mysqli_close($conn);
            ?>
        </table>
        <div class="col-xs-12 col-sm-4 col-sm-offset-4 col-md-4 col-md-offset-4 col-lg-4 col-lg-offset-4">
            <button class="btn btn-primary btn-lg" type="submit" name="submit">Submit</button>
        </div>    
    </form>

1 个答案:

答案 0 :(得分:0)

我能够弄清楚如何处理与学生还是客人的往来。我遍历$ _POST变量,并解析$ _POST数据的键,以确定我是在与学生还是与客人打交道。根据结果​​,我使用$ _POST数据的值将记录插入到出勤表中,以设置student_id或guest_id。这是代码:

<?php
  if(isset($_POST['submit'])){
    foreach($_POST as $key=>$value){
      if($key[0]=="s" && $key[1]=="t"){//$_POST data is a student and not guest or submit

        //Submit record to attendance table using the $value as the student_id
      }
      if($key[0]=="g"){//$_POST data is a student and not guest or submit

        //Submit record to attendance table using the $value as the guest_id
      }       
    }
  }
?>