错误消息多出现1

时间:2013-09-13 05:04:29

标签: php html string view interface

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

$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
}

enter image description here

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

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分: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
}