目前,我正在编制考勤表。在这个接口中,它由几行学生组成(从学生表中获取)。表中显示了学生名单。第一栏是否,第二栏在出生编号,第三STudent名称,第四次出勤。在列考勤中,有三个单选按钮,即PT、AT和MC,它们的值分别是存在的、缺失的和mc的。在表的外面,有一个提交按钮。老师需要点击学生的出勤率,不管是pt、at还是mc。老师需要为所有的学生点击。当老师点击提交按钮时,学生的出勤率将被插入到数据库中。当我在下面执行代码时,数据库中没有插入任何值。有人能帮我找出我哪里出了问题吗?在过去的两周里,我一直在编写这个代码,但仍然没有得到预期的结果。谢谢。
$getdata = mysql_query("select * from student where
class = '$class' order by name ") or die(mysql_query);
if (isset($_POST['submit'])){
$id = 1;
while($row = mysql_fetch_assoc($getdata)){
if(isset($_POST['a'.$id])) {
$status = $_POST['a'.$id];
if(!empty($status)){
if(($status == "present" || $status == "mc")){
$attend = 1;
}
else if($status == "absent"){
$attend = 0;
}
$query = "INSERT INTO attendance(birth_no, date, status, attend)
VALUES ('$birth_no','$date','$status','$attend')";
if($query_run = mysql_query($query)){
echo 'Insert attendance done';
}
else{
echo'Attendance not inserted.';
}
}
else{
echo 'Please enter all fields';
}
}
$id ++;
}
}
else {
?>
<form action="addattend.php" method = "POST">
<table>
<?php
$id = 1;
while($row = mysql_fetch_assoc($getdata)){
$birth_no= $row['birth_no'];
$name = $row['name'];
?>
<tr>
<td><center><?php echo $id ?></center></td>
<td><center><?php echo $date ?></center></td>
<td><center><?php echo $birth_no ?></center></td>
<td><center><?php echo $name ?></center></td>
<td>
<input type="radio" name="a<?php echo $id; ?>" value="present">PT
<input type="radio" name="a<?php echo $id; ?>" value="absent">AT
<input type="radio" name="a<?php echo $id; ?>" value="mc">MC
</td>
</tr>
<?php
$id ++;
} // end while
?>
</table>
<center><input type="submit" name="submit" value="Submit"></center>
</form> <!-- end the form here -->
<?php
} // end else
?>这就是我所指的界面(下图)。

每一行都有3个单选按钮。从数据库中检索了学生名单。教师需要为所有学生选择出勤率。使用一个单一的提交按钮,所有考勤将被插入到数据库中,包括birth_no、日期(由老师提前选择)以及在选定出勤后分配的$attend。
发布于 2013-09-11 16:54:44
当你说用主键代替$id时,我不明白
让我解释一下:当您将while循环与$id一起使用时,您会为每个while循环增加$id。如果您的表丢失了主键'1‘或任何一个主键呢?答:第一行可能与“1”的主键无关。让我用一些数据作进一步解释:
一个示例表:
student_id | name
-----------+--------
1 | John
3 | Jane
4 | Bob现在让我们使用sudo代码循环一段时间。
$id = 1;
while(row exists){
print row['name']." has primary key of $id";
$id++;
}这将打印类似以下内容的内容:
John has primary key of 1
Jane has primary key of 2
Bob has primary key of 3这是不正确的。
下面是我如何做这样的事情,这段代码可能需要为您的特定用途进行轻微的调整:
<?php
$getdata = mysql_query("select * from student where
class = '$class' order by name ") or die(mysql_query);
if (isset($_POST['submit'])){
while($row = mysql_fetch_assoc($getdata)){
//set the id equal to the primary key
$id = $row["birth_no"];
$date = date("Y-m-d");
if(isset($_POST['a'.$id])) {
$status = $_POST['a'.$id];
if(!empty($status)){
if(($status == "present" || $status == "mc")){
$attend = 1;
}
else {
$attend = 0;
}
$query = "INSERT INTO attendance(birth_no, date, status, attend)
VALUES ('$id','$date','$status','$attend')";
if($query_run = mysql_query($query)){
echo 'Insert attendance done';
}
else{
echo'Attendance not inserted.';
}
}
else{
echo 'Please enter all fields';
}
}
}
}
else {
?>
<form action="addattend.php" method = "POST">
<table>
<?php
while($row = mysql_fetch_assoc($getdata)){
$birth_no = $row['birth_no'];
$name = $row['name'];
?>
<tr>
<td><center><?php echo date("F j, Y") ?></center></td>
<td><center><?php echo $birth_no ?></center></td>
<td><center><?php echo $name ?></center></td>
<td>
<input type="radio" name="a<?php echo $birth_no; ?>" value="present">PT
<input type="radio" name="a<?php echo $birth_no; ?>" value="absent">AT
<input type="radio" name="a<?php echo $birth_no; ?>" value="mc">MC
</td>
</tr>
<?php
} // end while
?>
</table>
<center><input type="submit" name="submit" value="Submit"></center>
</form> <!-- end the form here -->
<?php
} // end else
?>https://stackoverflow.com/questions/18741184
复制相似问题