【发布时间】:2020-12-12 12:57:47
【问题描述】:
我有一个分为 2 个表格的页面。 1适用于活跃的人。 1 适用于不活跃的人。
活动和非活动部分基于数据库中的值。该表看起来像:
gebruiker_id naam aanwezig
----------------------------------
1 Mitch 0
2 Mitch2 1
如果 aanwezig 设置为 1,则名称将出现在左侧表格中。如果 aanwezig 的值设置为 0,则名称将出现在右表中。
我正在显示带有如下数据的表格:
在每个名称的后面,我创建了一个按钮,该按钮调用一个函数来将 aanwezig 的值更新为 0 或 1。这有效,但似乎总是更新最新的用户 ID,而不是我单击的用户 ID。例如,如果我按下 Mitch1 (UserID 1) 后面的按钮,Mitch2 (UserID2) 将被更新。这似乎是循环的问题,但我似乎无法弄清楚。
我已粘贴将用户从 Afwezig 放置到 Aanwezig 的代码(将其从 0 更改为 1)
<form method="POST">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-1">Naam</th>
<th class="col-md-2">Inchecken</th>
</tr>
</thead>
<tbody>
<!--Loop door alle afwezigen in het pand-->
<?php
foreach($afwezigheid as $afwezigheid){
$naam_afwezig = $afwezigheid['naam'];
$gebruiker_id = $afwezigheid['gebruiker_id'];
?>
<tr>
<td class="col-md-1"><?= $naam_afwezig ?></td>
<input type="hidden" name="aanmelden" value="<?= $gebruiker_id ?>">
<td class="col-md-2"><input class="btn btn-primary btn-sm" name="aanmeld_knop" value="<?= $gebruiker_id ?>" type="submit"></td>
</tr>
<?php
}//end loop
if(isset($_POST['aanmeld_knop'])){
$gebruikers_id = $_POST['aanmelden'];
$database->aanmelden($gebruikers_id, $aanwezig);
}
?>
</tbody>
</table>
</form>
以及数据库功能:
//functie die de aanwezigheid op 0 zet van een specifieke gebruiker
function aanmelden($gebruikers_id, $aanwezig) {
$sql = "UPDATE gebruikers SET aanwezig = :aanwezig WHERE gebruiker_id = :gebruiker_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':gebruiker_id', $gebruikers_id, PDO::PARAM_INT);
$sth->bindParam(":aanwezig", $aanwezig, PDO::PARAM_INT);
$sth->execute();
header("Refresh:0");
}
正如您在图片中看到的那样,gebruiker_id 使用正确。但是如果我想更新用户 1,用户 2 会收到更新。
我想知道哪里出了问题以及如何解决这个问题。
【问题讨论】: