<thead>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>Designation</th>
<th>M a r k A t t e n d a n c e</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT * FROM userdetails");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['userid']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['designation']; ?></td>
<td>
<select name="brandlist[]" id = "brandlist" class=="col-sm-2">
<option value="present">Present</option>
<option value="absent">Absent</option>
<option value="late">Late</option>
<option value="absconding">Absconding</option>
<option value="resigned">Resigned</option>
<option value="terminated">Terminated</option>
</select>
</td>
</tr>
<?php
}
}else{
?>
<tr><td colspan="5">No Employees Found...</td></tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-12">
<button type="submit" name = "submit">Submit</button>
</div>
</form>
</div>
</div>
</div>
这一切都很好,我可以看到屏幕上显示的行,我还可以选择下拉列表并选择出勤。选择提交后,下面的代码运行良好并将出勤率保存在表中。
<?php
if(isset($_POST['submit']));
{
$attendances = $_POST['brandlist'];
foreach($attendances as $item)
{
$query = "INSERT INTO attendance (attendancemarked) VALUES ('$item')";
$query_run = mysqli_query($conn, $query);
echo "<script language= 'JavaScript'>alert(' . $useriids . ');</script>";
}
}
?>
整个代码运行良好。我能够从 userdetails 表中获取数据并按行显示记录。我在每一行中添加了下拉列表,用于标记员工的出勤情况。当我提交时,出勤数组会保存所有标记的出勤,但用户 ID、用户名、名称不会保存。我希望这 3 个字段也存储在出勤表中。
您的代码存在几个问题:
$_POST
数据中读取这些值以下示例修复了这些问题。
表格显示: 在本节中,我将用户 ID 和名称添加为隐藏字段,并为出勤字段指定了一个更有意义的名称
<thead>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>Designation</th>
<th>M a r k A t t e n d a n c e</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT * FROM userdetails");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['userid']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['designation']; ?></td>
<td>
<input type="hidden" name="user[]" value="<?php echo $row['userid']; ?>">
<input type="hidden" name="designation[]" value="<?php echo $row['designation']; ?>">
<select name="attendance[]" id = "attendance" class=="col-sm-2">
<option value="present">Present</option>
<option value="absent">Absent</option>
<option value="late">Late</option>
<option value="absconding">Absconding</option>
<option value="resigned">Resigned</option>
<option value="terminated">Terminated</option>
</select>
</td>
</tr>
<?php
}
}else{
?>
<tr><td colspan="5">No Employees Found...</td></tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-12">
<button type="submit" name = "submit">Submit</button>
</div>
</form>
</div>
</div>
</div>
表单处理: 在这部分代码中,我添加了从所有相关
$_POST
值读取的代码,将循环更改为 for
,以便循环计数器可用于从每个值中选择关联值提交的数组(每个字段都有一个),并使用准备好的语句和参数来正确构建查询。
if(isset($_POST['submit']));
{
$attendances = $_POST['attendance'];
$userIDs = $_POST['user'];
$designations = $_POST['designation'];
for ($i = 0; $i < count($attendances); $i++)
{
$query = "INSERT INTO attendance (attendancemarked, userID, designation) VALUES (?, ?, ?)";
$conn->prepare($query);
$conn->execute([$attendances[$i], $userIDs[$i], $designations[$i]);
echo "Inserted attendance record for ".htmlspecialchars($userIDs[$i])."<br>";
}
}