我正在使用考勤管理系统,但无法理解。请帮助我
<form method="post">
<div class="table-responsive py-4">
<table class="table table-flush" id="datatable-basic">
<thead class="thead-light">
<tr>
<th>Name</th>
<th>Father's Name</th>
<th>Hall Ticket</th>
<th>Department</th>
<th>Attendace</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST["search_students"])){
$department=$_POST['department'];
$sql = "SELECT * FROM student_info WHERE department='$department'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["father_name"]."</td>";
echo "<td>".$row["hall_ticket"]."</td>";
echo "<td>".$row["department"]."</td>";
echo "<td>
Present <input type='radio' name='attendance[".$row['hall_ticket']."][".$row['department']."]' value='Present'>
Absent <input type='radio' name='attendance[".$row['hall_ticket']."][".$row['department']."]' value='Absent'>
</td>";
echo "</tr>";
}
}
}
?>
</tbody>
</table>
</div>
<div class="col-lg-12 mb-3">
<button type="submit" name="mark_attendance" class="btn btn-success">Mark Attendance</button>
</div>
</form>
</div>
<?php
if(isset($_POST["mark_attendance"])){
$attendance=$_POST['attendance'];
foreach ($attendance as $key => $value) {
if($value=="Present") {
$query="Insert into attendance (hall_ticket,attendance) values ('$key','Present')";
$insertAttendance=$conn->query($query);
}
else {
$query="Insert into attendance (hall_ticket,attendance) values ('$key','Absent')";
$insertAttendance=$conn->query($query);
}
}
if($insertAttendance){
echo "Attendance Updated Sucessfully";
}
}
?>
</div>
在这里,我想将2个密钥作为礼堂票价值和部门价值存储到我的数据库中。
attendance[".$row['hall_ticket']."][".$row['department']."]
如何使用两个变量存储到我的数据库中。我想将每个餐桌人员的出席情况与他们的部门和礼堂一起存储。如果我从名字中删除部门,而只是使用$ key来存储值,我将获得资格证。
答案 0 :(得分:1)
尝试以下代码:
HTML代码(已创建虚拟数据以供参考):
<form action='new.php' method="POST" >
Presnt<input type="radio" name="attendance[111][555]" value="present"><br>
Absent<input type="radio" name="attendance[111][555]" value="absent"><br><br>
Presnt<input type="radio" name="attendance[222][555]" value="present"><br>
Absent<input type="radio" name="attendance[222][555]" value="absent"><br><br>
Presnt<input type="radio" name="attendance[333][555]" value="present"><br>
Absent<input type="radio" name="attendance[333][555]" value="absent"><br>
<input type="submit">
</form>
PHP代码:
<?php
$attendance=$_POST['attendance'];
$data = "";
foreach ($attendance as $key => $value) {
$department = array_keys($value)[0];
$data.="('". $key ."','". $department ."', '" . $value[$department] . "'),";
}
$query = "INSERT INTO attendance (hall_ticket,department,attendance) VALUES " .
rtrim($data, ",");
// Your Query will look like:
// Query = "INSERT INTO attendance (hall_ticket,department,attendance)
// VALUES ('111','555', 'present'),
// ('222','555', 'present'),
// ('333','555', 'absent')";
// Now execute your query
$conn->query($query);
echo $sql;
?>
注意:
答案 1 :(得分:0)
所以您$attendance
是
$attendance=array(
$row['hall_ticket'] => array(
$row['departement']"=> [" present " OR "absence"]
);
我所了解的
我的解决方法是
foreach ($attendance as $hall => $departements) {
foreach($departements as departement => $value){
$req="INSERT INTO attendance (hall_ticket,department,attendance)
values ('".$hall. "','". $departement ."' ,'". $value ."')";
$insertAttendance=mysqli_query($dataBaseConnect,$req);
}