2016-12-03 81 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) –