0

我正在编写此代码以插入出勤。这段代码很好。但是有一些输出我不希望它再次出现两次。我不知道我到底需要在哪里更正它。希望有人可以重新排列我的代码,以便输出不会像下面的图片。

$class = getfield('class');
$getdata = mysql_query("select * from student where class = '$class' order by name ")     or die(mysql_query);
$result = "select birth_no from student where class = '$class'";
$getbirth = mysql_query($result) or die(mysql_error());

if(isset($_POST['date']))
{
    $_SESSION['date'] = $_POST['date'];
    $date = $_POST['date'];
    if(!empty($date))
    {

        while($row = mysql_fetch_assoc($getdata))
        {
            $id = $row["birth_no"];
            $query = "SELECT * FROM attendance WHERE date = '$date' and birth_no = '$id'";
            $query_run = mysql_query($query);
            if(mysql_num_rows($query_run)==0)
            {
                    //start now
                    //start here
                    if (isset($_POST['submit']))
                    {

                            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(ID, 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="addattend1.php" method = "POST">

                            <?php
                                $name = $row['name'];
                            ?>
                                <tr>
                                    <td><center><?php echo $date ?></center></td>
                                    <td><center><?php echo $id ?></center></td>
                                    <td><center><?php echo $name ?></center></td>
                                    <td><center>
                                        <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
                                        </center>
                                    </td>
                                </tr>
                            <?php

                    } // end else
                ?>
                    <?php //end here


            }//end if           
            else
            {
                echo '<br><br>Attendance for date <font color="#FF0000" ><b><u>'.$date.'</u></b></font> is already inserted!<br><br>';
            }
        }//end while
            ?>
                        </table>
                        <br /><br />
                        <center><input type="submit" name="submit" value="Submit"></center><br /><br />
                        </form> <!-- end the form here -->
                        <?php 
    }//if not empty
    else
    {
        ?>
        <br/
        <font size="3" color color="#FF0000"><?php echo 'Please pick a date first before proceed!'  ?>  </font><br>
        <?php
    }
}//if isset date
else
{
    ?><br/><br/>
    <form action="addattend.php" method = "POST">
    Date :<br><br ><input type="date" name = "date">
    <input type="submit" value="Submit"><br /><br />
    </form> <?php
}

在此处输入图像描述

如上图所示,由于有 2 个学生,因此出现了两次“已插入日期的出勤”。如果有 5 个学生,则该字符串将出现 5 次,依此类推。我真正想要的是,我希望它只出现一次,并且带有提交按钮的表格不会显示在那里。

请帮我。谢谢你。

4

1 回答 1

0

不是最聪明的,但它应该像这样使用错误数组......

诀窍是声明一个错误数组。对于您可以执行的每个错误

$errors[$date] = "Your error";

因此每个日期的错误都是唯一的,因为它会覆盖给定日期的现有错误。最后用 . 输出错误implode

$class = getfield('class');
$getdata = mysql_query("select * from student where class = '$class' order by name ")     or die(mysql_query);
$result = "select birth_no from student where class = '$class'";
$getbirth = mysql_query($result) or die(mysql_error());

if(isset($_POST['date']))
{
    $_SESSION['date'] = $_POST['date'];
    $date = $_POST['date'];
    if(!empty($date))
    {
        $errors = array(); // declare error array

        while($row = mysql_fetch_assoc($getdata))
        {
            $id = $row["birth_no"];
            $query = "SELECT * FROM attendance WHERE date = '$date' and birth_no = '$id'";
            $query_run = mysql_query($query);
            if(mysql_num_rows($query_run)==0)
            {
                    //start now
                    //start here
                    if (isset($_POST['submit']))
                    {

                            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(ID, 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="addattend1.php" method = "POST">

                            <?php
                                $name = $row['name'];
                            ?>
                                <tr>
                                    <td><center><?php echo $date ?></center></td>
                                    <td><center><?php echo $id ?></center></td>
                                    <td><center><?php echo $name ?></center></td>
                                    <td><center>
                                        <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
                                        </center>
                                    </td>
                                </tr>
                            <?php

                    } // end else
                ?>
                    <?php //end here


            }//end if           
            else
            {
                $errors[$date] = '<br><br>Attendance for date <font color="#FF0000" ><b><u>'.$date.'</u></b></font> is already inserted!';
                //echo '<br><br>Attendance for date <font color="#FF0000" ><b><u>'.$date.'</u></b></font> is already inserted!<br><br>';
            }
        }//end while

        echo "</table>"; // i put it before output the errors, by the way i cannot find any beginn table
        if(count($errors) > 0) {
            echo implode('<br><br>',$errors);
        }
            ?>

                        <br /><br />
                        <center><input type="submit" name="submit" value="Submit"></center><br /><br />
                        </form> <!-- end the form here -->
                        <?php 
    }//if not empty
    else
    {
        ?>
        <br/
        <font size="3" color color="#FF0000"><?php echo 'Please pick a date first before proceed!'  ?>  </font><br>
        <?php
    }
}//if isset date
else
{
    ?><br/><br/>
    <form action="addattend.php" method = "POST">
    Date :<br><br ><input type="date" name = "date">
    <input type="submit" value="Submit"><br /><br />
    </form> <?php
}
于 2013-09-13T05:29:57.967 回答