如何更新来自php的循环数据系列?

时间:2017-09-16 15:39:17

标签: php

我目前正在做一个学生管理系统,我需要做一个更新学生成绩的功能。由于所有结果都是来自数据库的循环,我很困惑如何使用相同的循环更新数据。我想在单击提交按钮时将所有新标记(相同或更新的值)更新到数据库中。

我通过互联网做了一些研究,但他们的方式与我的完全不同。  这是我的代码。

                  <table class="table table-responsive" style="width: 100%;">
                        <tbody>
                          <tr>
                            <th>No</th>
                            <th>Student ID</th>
                            <th>Student Name</th>
                            <th>Student Result</th>
                          </tr>
                          <?php
                                $sql="select * from class INNER JOIN enrollment ON class.class_id=enrollment.class_id INNER JOIN student ON enrollment.stud_id=student.stud_id INNER JOIN registration ON student.reg_id=registration.reg_id WHERE class.sup_id='$sup_id' and enrollment.enrollment_status='Active' ";
                                $result=mysqli_query($con,$sql);

                                $i=1;
                            if(mysqli_num_rows($result)!=0)
                            {
                                while($row=mysqli_fetch_assoc($result))
                                {
                            ?>

                          <tr>
                                <td><?php echo $i++ ;?></td>
                                <td><?php echo $row['stud_code']; ?></td>
                                <td><?php echo $row['stud_name']; ?></td>
                                <td><input type="text" class="form-control pw" value="<?php echo $row['class_gpa']; ?>" name="studgpa" id="studgpa"></td>
                                <td><input type="hidden" name="enrollid" value="<?php echo $row['enrollment_id']; ?>"></td>
                          </tr>
                            <?php

                                }
                            }
                            else
                            {
                            ?>
                            <tr>
                              <td colspan='4' style="text-align:center; font-size:1.4em;">No record found!</td>
                            </tr>
                            <?php
                            }
                                ?>

                        </tbody>
                    </table>
<button type="submit" name="subbtn" class="btn btn-default btn-block addcbtn" style="background-color: #e4e4e4; "><i class="glyphicon glyphicon-send" ></i>Update</button>

if(isset($_POST['subbtn']))
        {

            $gpa=$_POST['studgpa'];
            $enrollid=$_POST['enrollid'];

            $sql2="select * from class INNER JOIN enrollment ON class.class_id=enrollment.class_id INNER JOIN student ON enrollment.stud_id=student.stud_id INNER JOIN registration ON student.reg_id=registration.reg_id WHERE class.sup_id='$sup_id' and enrollment.enrollment_status='Active' ";
            $result2=mysqli_query($con,$sql2);

            while(mysqli_fetch_assoc($result2))
            {

                mysqli_query($con,"update enrollment set class_gpa='$gpa' where enrollment_id='$enrollid");

            }

        }

1 个答案:

答案 0 :(得分:2)

有一个技巧可以从表单中获取所有值

您的HTML代码

<tr>
    <td><?php echo $i++ ;?></td>
    <td><?php echo $row['stud_code']; ?></td>
    <td><?php echo $row['stud_name']; ?></td>
    <td><input type="text" class="form-control pw" value="<?php echo $row['class_gpa']; ?>" name="studgpa" id="studgpa"></td>
    <td><input type="hidden" name="enrollid" value="<?php echo $row['enrollment_id']; ?>"></td>
</tr>

应该是

<tr>
    <td><?php echo $i++ ;?></td>
    <td><?php echo $row['stud_code']; ?></td>
    <td><?php echo $row['stud_name']; ?></td>
    <td><input type="text" class="form-control pw" value="<?php echo $row['class_gpa']; ?>" name="studgpa[<?php echo $row['stud_code']; ?>]" id="studgpa"></td>
    <td><input type="hidden" name="enrollid[<?php echo $row['stud_code']; ?>]" value="<?php echo $row['enrollment_id']; ?>"></td>
</tr>

在PHP上,您可以使用以下命令获取循环中的所有值:

// both will be 2 dimension array
$_POST['studgpa']
$_POST['enrollid']

// For getting `studgpa` of student code `abc`
if (!empty($_POST['studgpa']) && !empty($_POST['studgpa']['abc'])) {
    $tmp_stdgpa = $_POST['studgpa']['abc'];
}

与enrollid相同的事情。还有一件事,总是把PHP代码放在HTML输出之上。希望这对你有所帮助!