在表中显示数据以进行更新

时间:2016-12-03 17:03:50

标签: php mysql

我有一个名为学生学生的表格,我在表格中显示,以便能够更新他们的数据。

我还有一个名为Groepen(groups)的表,有些学生是一个小组的成员。他们所属的小组也会显示出来。还有一个选项可以让学生成为小组的一部分。

看起来像这样: 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 个答案:

答案 0 :(得分:1)

在此处查看此行,

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

不是在<option>元素中添加额外<select>,而是在while循环的每次迭代中检查当前组ID是否与学生的组ID匹配,然后进行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>