在phpmyadmin att表中插入多条记录,在点击提交按钮后保持所有信息相同除了出勤和学生ID。它只占用最后一行而没有出勤状态。我从数据库中取出考勤表。我被困在这里在我的第一个php项目中。
教师小组
<form action="<?php $PHP_SELF ?>" method="post">
<li><b>Select Courses :</b> <select name="courses" id="courses" class="form-
control action" >
<option value="">Courses</option>
<?php echo $courses; ?></select></li><br/>
<li><b>Select Semesters :</b> <br><select name="semesters" id="semesters"
class="form-control action" >
<option value="">Semesters</option></select></li><br/>
<li><b>Select Subjects :</b><br> <select name="subjects" id="subjects"
class="form-control " >
<option value="">Subjects</option></select>
<br/>
<li><b>Session No:<b><br/><input type="text" name="session_no"
id="session_no" placeholder ="Session No" class="form-control">
<br>
<label class="control-label" for="date">Date</label>
<br/>
<input class="form-control" id="date" name="date" placeholder="DD/MM/YYYY"
type="date"/>
<br/>
<li><b>Enter time:</b></li><br/>
From :<input type="time" name="time_from" id="time_from" class="form-
control"/> </li><li>
<br/>
To :<input type="time" name="time_to" id="time_to" class="form-control"/>
</li></div>
<br>
<li><label class="btn btn-info">Display attendance sheet</label> </li>
<br/>
//attendance table
<table id = "table" class = "table table-bordered">
<thead class = "alert-info">
<tr>
<th>Student ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Present</th>
<th>Absent</th>
</tr>
</thead>
<tbody>
<?php
$q_student = $conn->query("SELECT * FROM `student`")
or die(mysqli_error());
while($f_student = $q_student->fetch_array()){
?>
<tr>
<td><input name="student_no" value="<?php echo
$f_student['student_no']?>" class="form-control"
readonly/></td>
<td><?php echo $f_student['firstname']?></td>
<td><?php echo $f_student['lastname']?></td>
<td><input type="radio" name="attendance[<?php echo
$f_student['student_no']; ?>]" value="1"
class="form-control"></td>
<td><input type="radio" name="attendance[<?php echo
$f_student['student_no']; ?>]" value="0"
class="form-control"></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
//Insert Record into attendancetable
if(isset($_POST['submit']))
{
foreach ($_POST['attendance'] as $attendance){
$faculty_id =
mysqli_real_escape_string($conn,$_SESSION['faculty_id']);
$student_no =
mysqli_real_escape_string($conn,$_POST['student_no']);
$courses = mysqli_real_escape_string($conn,$_POST['courses']);
$semesters = mysqli_real_escape_string($conn,$_POST['semesters']);
$subjects = mysqli_real_escape_string($conn,$_POST['subjects']);
$session_no = mysqli_real_escape_string($conn,$_POST['session_no']);
$date = mysqli_real_escape_string($conn,$_POST['date']);
$time_from = mysqli_real_escape_string($conn,$_POST['time_from']);
$time_to = mysqli_real_escape_string($conn,$_POST['time_to']);
$attendance=var_dump($_POST['attendance']);
$conn=mysqli_connect("localhost","root","","db_sars");
$ins="INSERT INTO att( faculty_id,student_no,courses,semesters,subjects,session_no,date,time_from,time_to,attendance_status)
VALUES ('$faculty_id','$student_no','$courses','$semesters','$subjects','$session_no','$date','$time_from','$time_to','$attendance')";
}
if(@mysqli_query($conn,$ins))
{
echo "inserted";
}
else
{
die(@mysqli_error());
}
}
?><input type="submit" name="submit" value="Submit Form" class="btn btn-info"></form>
答案 0 :(得分:0)
在您发布的代码中,由于使用$_POST['attendance']
,您未将出勤数据($attendance
)分配给var_dump
变量。
您写道:
$attendance=var_dump($_POST['attendance']);
来自var_dump的php手册:
返回值
没有返回任何值。
相反,请尝试格式化此行,就像您完成所有其他操作一样:
$attendance = mysqli_real_escape_string($conn,$_POST['attendance']);
您似乎在前端表中打印每个学生ID。我认为这将是一个更好的领域循环。但是,使用您当前的前端代码,它会为每个学生ID创建一个新的post变量。相反,你可能想要这个:
<td><input name="student_no" value="StudentID[<?php echo $f_student['student_no']?>]" class="form-control" readonly/></td>
请注意,这会将学生ID转换为学生ID数组,而不是每个学生ID的大量不同变量。
然后为你的foreach:
foreach ($_POST['StudentID'] as $StudentID) {
现在,每次迭代都会更新 $StudentID
,并允许您在其他数组中查找相应的索引。您可以将相关的行更改为:
$student_no = mysqli_real_escape_string($conn,$StudentID); // The student ID is set by as in foreach
$StudentAttendance = $_POST['attendance'][$StudentID]; // Get the attendance status by looking up the student by student id in the attendance array
$attendance=mysqli_real_escape_string($conn,$StudentAttendance); // Escape input to avoid table disasters (reference Little Bobby Tables)
我还没有对你的代码进行测试,并且明白还有很多事情要发生。但是,最好一次解决一件事。 您需要做的是创建一个循环来覆盖每个学生,并且您要收集的每个字段都应该作为一个数组发布,其中包含每个学生的学生ID索引。
使用var_dump
对于理解变量包含的内容很有用(例如,如果您将var_dump输出与问题一起发布,它将帮助我们查看变量包含的内容)。如果您有错误消息,请发布,因为它有助于指导我们正确的方向,因为我们会帮助您。