2017-09-16 52 views
1

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

      } 

     } 

回答

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']; 
} 

同一件事情报名。还有1件事,总是把PHP代码放在HTML输出以上。希望这对你有所帮助!