2

目前,我正在处理出勤表。在此界面中,它由学生行(从学生表中获取)组成。学生名单见表。第一列为否,第二列为出生编号,第三列为学生姓名,第四列为出勤。在考勤列中,有 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 个单选按钮。学生名单是从数据库中检索出来的。教师需要选择所有学生的出勤率。使用单个提交按钮,所有出勤都将插入到数据库中,出生编号、日期(由老师较早选择)以及在选择出勤后分配的 $attend。

4

1 回答 1

0

当您说使用主键而不是 $id 时,我不明白

让我解释一下:当你使用while循环时,$id你会增加$idfor eachwhile循环。如果您的表缺少主键“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
?>
于 2013-09-11T16:54:44.447 回答