在表格中显示数据以进行更新 [英] Display data in a table for updating purposes
问题描述
我有一个名为学生的学生"的表格,我以表格的形式显示该表格,以便能够更新他们的数据.
I have a table named students of students that I display in a form to be able to update their data.
我还有一个名为Groepen(小组)的表格,一些学生是小组的一部分.他们所属的组也会显示出来.还有一个选择选项可以使学生成为小组成员.
I also have a table named Groepen (groups), and some students are part of a group. The group they are part of is also displayed. And there is a select option to make a student part of a group.
看起来像这样:
如果某个学生属于某个小组,而我例如更新了他的电话号码,那么更新后他就不在该小组中.
If a student is part of a group and I update for instance, his phone number, he isn't in the group anymore after the update.
如何防止这种情况?
我的代码是
<?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);
}
?>
推荐答案
在此处查看此行,
<option value="><?$student_list['groepid']; ?>"><?php echo $student_list['groepsnaam']; ?></option>
^ see this closing bracket
不是在<select>
元素中添加额外的<option>
,而是在while
循环的每次迭代中检查当前组ID是否与学生的组ID匹配,并相应地将其设置为selected
,例如这个:
Instead of putting an extra <option>
in the <select>
element, in each iteration of while
loop check if the current group id matches with the student's group id or not, and make it selected
accordingly, like this:
<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>
这篇关于在表格中显示数据以进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!