2016-12-03 80 views
1

我有一张表格,名为学生,我在表单中显示可以更新他们的数据。在表格中显示数据用于更新目的

我也有一个名为Groepen(组)的表,有些学生是一个组的一部分。他们所属的小组也被显示出来。并且有一个选择让学生成为一个组的一部分。

是这样的: enter image description here

如果学生一组的一部分,我更新的情况下,他的电话号码,他是不是该组中再更新后。

我该如何预防?

我的代码是

<?php 

    $q = "SELECT * FROM students LEFT JOIN Groepen ON Groepen.groep_id=students.groep WHERE students.uid = '$user[uid]' ORDER BY st_last ASC"; 
       $r = mysqli_query($dbc, $q); 

       if (mysqli_num_rows($r) > 0) { 

        echo " 

        <p>Om gegevens te wijzigen of aan te vullen kunt u gewoon in het desbetreffende veld typen en vervolgens op 'update' klikken.</p></br></br> 

        <table class='table'> 

         <thead> 
          <tr> 
           <th>Naam</th> 
           <th>Voornaam</th> 
           <th>Graad</th> 
           <th>Telefoon</th> 
           <th>Gsm</th> 
           <th>Email</th> 
           <th>Lid van groep</th> 
           <th>Toevoegen aan/verwijderen uit groep</th> 
          </tr> 
          </thead>"; 

       while($student_list = mysqli_fetch_assoc($r)) { ?> 



     <tbody> 
     <form action="" method="POST" role="form"> 
     <tr> 
    <input type="hidden" value="<?php echo $student_list['sid']?>" name="sid" /> 
     <td width="8%"><input type="text" class="form-control" name="st_last" value="<?php echo $student_list['st_last']; ?>" /></td> 
     <td width="6%"><input type="text" class="form-control" name="st_first" value="<?php echo $student_list['st_first']; ?>"/></td> 
     <td width="2%"><input type="text" class="form-control" name="graad" value="<?php echo $student_list['graad']; ?>"/></td> 
     <td width="7%"><input type="text" class="form-control" name="vaste_telefoon" value="<?php echo $student_list['vaste_telefoon']; ?>"/></td> 
     <td width="7%"><input type="text" class="form-control" name="gsm" value="<?php echo $student_list['gsm']; ?>"/></td> 
     <td width="10%"><input type="text" class="form-control" name="email" value="<?php echo $student_list['email']; ?>"/></td> 
     <td width="8%" style="padding-top: 15px;"><?php echo $student_list['groepsnaam']; ?></td> 
     <td width="7%"><div class="form-group"> 


       <select class="form-control" name="groep_id" id="groep"> 

       <option value="><?$student_list['groepid']; ?>"><?php echo $student_list['groepsnaam']; ?></option> 

        <?php 

         $q2 = "SELECT * FROM Groepen ORDER BY groepsnaam ASC"; 
         $r2 = mysqli_query($dbc, $q2); 

         while($groep_list = mysqli_fetch_assoc($r2)) { 

          ?> 

          <option value="<?php echo $groep_list['groep_id']; ?>"><?php echo $groep_list['groepsnaam']; ?></option> 
        <?php } ?> 


       </select> 

      </div></td> 
     <td width="12%"> <div class="btn-group" role="group" aria-label="..."> 

      <button type="submit" name="updatell" class="btn btn-warning">Update</button> 
      <button type="submit" name="deletell" class="btn btn-danger">Delete</button></td> 
     </tr> 
     </form> 
    </tbody> 


       <?php } } 

       else { 
        echo "U hebt nog geen leerlingen toegevoegd."; 
       } 

       ?> 
       </table> 

而且更新查询:

<?php 
    if(isset($_POST['updatell'])) { 
     $qupdatestudent = "UPDATE students SET st_last='$_POST[st_last]', st_first='$_POST[st_first]', groep='$_POST[groep_id]', graad='$_POST[graad]', vaste_telefoon='$_POST[vaste_telefoon]', gsm='$_POST[gsm]', email='$_POST[email]' WHERE sid='$_POST[sid]'"; 
     $r2 = mysqli_query($dbc, $qupdatestudent); 

    } 

    if(isset($_POST['deletell'])) { 
     $deletestudent = "DELETE FROM students WHERE sid='$_POST[sid]'"; 
     $r3 = mysqli_query($dbc, $deletestudent); 
    } 
?> 

回答

1

看到这里这条线,

<option value="><?$student_list['groepid']; ?>"><?php echo $student_list['groepsnaam']; ?></option> 
      ^see this closing bracket 

而不是把一个额外<option><select>元素,在每次迭代while循环检查c urrent组编号与学生的组编号匹配或不匹配,并相应地使其selected,如下所示:

<select class="form-control" name="groep_id" id="groep"> 
    <?php 

     $q2 = "SELECT * FROM Groepen ORDER BY groepsnaam ASC"; 
     $r2 = mysqli_query($dbc, $q2); 
     while($groep_list = mysqli_fetch_assoc($r2)) { 
     ?> 
      <option value="<?php echo $groep_list['groep_id']; ?>"<?php if($groep_list['groep_id'] == $student_list['groepid']){ echo " selected='selected'"; } ?>><?php echo $groep_list['groepsnaam']; ?></option> 
     <?php 
     } 
    ?> 
</select> 
+0

非常感谢!作品。不知道选择='选择'的解决方案。 –

+0

@BertLietaert很高兴我能帮到你。如果解决了您的问题,请*接受*答案。 [如何接受Stack Overflow的答案?](http://meta.stackexchange.com/a/5235) –