因此,作為前言 - 我正在嘗試在我的網站上實作“標記出勤”功能,在該功能中,我將所有注冊學生列印在一個表格中(包含在 中) - 每個學生都有一個“出席/缺席”單選按鈕和一旦管理員選擇了他的首選選項,他按“提交”,表格應標記所有學生出席或缺席,即插入多行(等于表中學生總數)及其出勤狀態,即缺席或出席。
以下是代碼的 HTML 部分(與一些 PHP 混合):
<form action="adminmarkattendance.php" method="post">
<div class="row">
<div class="col">
<div class="card bg-default shadow">
<div class="card-header bg-transparent border-0">
<div class="form-inline">
<div class="col-lg-6">
<h3 class="text-white mb-0">Registered Students</h3>
</div>
<div class="col-lg-6">
<div class="form-group">
<input style="width: 100%;" class="form-control" name="attendancedate" type="date" required>
</div>
</div>
</div>
</div>
<div class="table-responsive" style="overflow-y: scroll; height: 600px;">
<table class="table align-items-center table-dark table-flush">
<thead class="thead-dark">
<tr>
<th scope="col" class="sort" data-sort="name">Avatar</th>
<th scope="col" class="sort" data-sort="name">Student Name</th>
<th scope="col" class="sort" data-sort="status">Phone Number</th>
<th scope="col" class="sort" data-sort="status">Age</th>
<th scope="col" class="sort" data-sort="status">Gender</th>
<th scope="col" class="sort" data-sort="status">Address</th>
<th scope="col" class="sort" data-sort="status">Action</th>
</tr>
</thead>
<tbody class="list">
<?php
foreach ($getRows as $row) {
$i = 0; ?>
<tr>
<td>
<img src="<?php echo '../profileImages/' . $row['profile_image'] ?>" width="45" height="45" alt="">
<input type="hidden" name="id" value="<?php echo $row['student_id']; ?>">
</td>
<th scope="row">
<div class="media align-items-center">
<div class="media-body">
<span class="name mb-0 text-sm"><?php echo $row['fname'] . ' ' . $row['lname']; ?></span>
</div>
</div>
</th>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['phonenumber']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['age']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['gender']; ?></span>
</span>
</td>
<td>
<span class="badge badge-dot mr-4">
<span class="status"><?php echo $row['address']; ?></span>
</span>
</td>
<td>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="options" value="present" id="option1" autocomplete="off" checked> Present
</label>
<label class="btn btn-secondary">
<input type="radio" name="options" value="absent" id="option2" autocomplete="off"> Absent
</label>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" name="submit" style="width: 100%;" class="btn btn-warning">Mark Attendance</button>
</div>
</form>
PHP:
if (isset($_POST["submit"])) {
$student_id = $_POST["id"];
$date = $_POST['attendancedate'];
$date = date('Y-m-d', strtotime($date));
$status = $_POST['options'];
$queryInsert = $conn->prepare("INSERT
into attendance
(
student_id,
date,
status
)
values
(
$student_id,
'$date',
'$status'
)
");
$queryInsert->execute();
echo "<script> location.replace('adminmarkattendance.php'); </script>";
}
正如您可能在代碼中看到的那樣 - 我只是想插入學生的 ID、日期和他的當前/缺席狀態。
現在,當我按下提交時,只有 1 個學生的資訊被插入到出勤表中。通常是最后一個學生。這不是我想要的 - 我希望在按下提交時插入 X 行,其中 X 等于表中的學生人數。
我如何實作這一目標?感謝您的閱讀。
uj5u.com熱心網友回復:
您需要的是提交整個資料,然后像這樣回圈遍歷它
<input type="hidden" name="id[]" value="<?php echo $row['student_id']; ?>">
<input type="radio" name="options[]" value="absent" id="option2" autocomplete="off"> Absent</label>
然后
if (isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["id"]);$i )
{
$student_id = $$_POST["id"][$i]];
$date = $_POST['attendancedate'];
$date = date('Y-m-d', strtotime($date));
$status = $_POST['options'][$i]];
$queryInsert = $conn->prepare("INSERT into attendance(student_id,date,status)
values ($student_id,'$date','$status')");
$queryInsert->execute();
}
}
注意 根據需要調整代碼。
uj5u.com熱心網友回復:
可悲的是,我還不能評論答案。我覺得我確實對Adib Javed的答案(后端部分)有所改進。
最好 - 不要回圈資料庫請求,并嘗試發出單個請求。因此,您可以回圈創建請求,并在回圈結束時發送該單個請求:
if (isset($_POST["submit"]))
{
$query = "INSERT into attendance(student_id,date,status) values";
for($i=0;$i<count($_POST["id"]);$i )
{
$student_id = $$_POST["id"][$i]];
$date = $_POST['attendancedate'];
$date = date('Y-m-d', strtotime($date));
$status = $_POST['options'][$i]];
$query .= " ($student_id,'$date','$status'),";
}
substr_replace($query ,";", -1);
$queryInsert = $conn->prepare($query);
$queryInsert->execute();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/424351.html
上一篇:【CentOS7離線ansible-playbook自動化安裝CDH5.16(內附離線安裝包地址,及自動化腳本)】
