php - 我从 mysql 获取数据并勾选它并将其发送回 mysql 记录只保存一个 id 为 3 的学生
问题描述
我从mysql获取数据并将其显示在表中现在我试图通过选中复选框将其发送到每个学生的数据库,那么我应该怎么做才能将所有表数据发送到具有不同学生ID的msql数据库中
<form class="col-md-12" action="Attendance.php" method="post">
<table id="example" class="myclass table table-striped" />
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Father Name</th>
<th><label><input type="checkbox" id="selectAll" name="chbox[]"> All Present </label></th>
</tr>
</thead>
<tbody>
<?php
$SrNo = 0;
global $dbManager;
$sql = "SELECT * FROM studentinfo";
$stmt = $dbManager->query($sql);
while($DataRows = $stmt->fetch()){
$RollNo = $DataRows['id'];
$FullName = $DataRows['fullname'];
$FatherName = $DataRows['fathername'];
?>
<tr>
<td><?php echo $RollNo; ?></td>
<td><?php echo $FullName; ?></td>
<td><?php echo $FatherName; ?></td>
<td><input type="checkbox" name="chbox[]" value="1" /></td>
</tr>
</tbody>
<?php } ?>
<tfoot>
<?php
if(isset($_POST['Submit'])){
$StudentRollNo = $RollNo;
$Attendance = $_POST['chbox'];
// date_default_timezone_set("Asia/Kabul");
$CurrentTime = time();
$DateTime = strftime("%B-%d-%Y %H:%M:%S", $CurrentTime);
if(empty($Attendance)){
$_SESSION['ErrorMessage'] = "Please filled all fields";
RedirectTo("Attendance.php");
}
else{
// the sql code is here.
global $dbManager;
foreach ($Attendance as $key => $value) {
$sql = "INSERT INTO attendance(sid, subjectid, classid, attendance, datetime) VALUES(:studentId,'1','2', :attendancE, :dateTime)";
$stmt = $dbManager->prepare($sql);
$stmt->bindValue(':studentId',$StudentRollNo);
$stmt->bindValue(':attendancE',$value);
$stmt->bindValue(':dateTime',$DateTime);
$Execute = $stmt->execute();
if($Execute){
$_SESSION['SuccessMessage'] = "Attendance Submited Successfully.";
RedirectTo("Attendance.php");
}
else{
$_SESSION['ErrorMessage'] = "Something went wrong. Try Again!";
RedirectTo("Attendance.php");
}
}
}
?>
<tr>
<td>
<i class="fa fa-check"></i>
<input type="submit" name="Submit" value="Save Attendance">
</td>
</tr>
</tfoot>
</table>
</form>
Mysql 数据库:问题是记录只保存一个学生。
解决方案
将表带入 while 循环中,从数据库中获取值,然后为复选框动态分配学生的值value
你可以做
value="<?php echo $RollNo; ?>";
就像您回显其他值一样,将回显添加到复选框的值字段中,以便动态分配给每个复选框的值
推荐阅读
- javascript - 如何使用基于其他数组的特定动态键过滤/减少对象数组
- reactjs - 测试抱怨找不到组件,但它在那里
- sql - 从 id A 中选择最新数据
- notepad++ - notepad++ 在文件中查找:“查找结果”面板 - 显示与搜索条件匹配的行之前/之后的行
- java - Tomcat 战争 - 提供 css 文件?
- reactjs - 状态在本机反应中没有改变
- swift - UICollectionViewCompositionalLayout 垂直中心组
- c - C:函数do删除通用链表上的节点
- dataframe - 如何按列值在pyspark df中添加更多行
- http - Nuget 包无法恢复并出现错误 - HTTP 请求在 100000 毫秒后超时