1

我有一个供用户申请许可证的系统......当他们选择他们希望退休的许可证时,我的系统应该能够检查是否符合标准,或者他们之前是否已经申请过该许可证,以防止垃圾邮件。

但是,我有一个大问题。如果我把header('Location:s_success.php');警报不会出现。例如,由于用户可以选择 1 个或 2 个或更多许可,.

设想

假设选择一个他们有资格申请的许可证和另一个他们还没有资格的许可证。

预期结果

在这种情况下,应该发生的是系统应该将他们带到成功页面,同时提醒他们他们不符合资格的其他许可证不成功。

但意想不到的结果:

他们的系统只是进入成功页面,而不会警告用户不合格的许可不成功。

这是我的代码

<?php
session_start();
include'Connections/database.php';
$conn = dbConnect ();
    if (! $conn)
    die("Couldn't connect to MySQL");
    
    $user = $_SESSION['eid'];
    $selectedPermit=$_POST['cat'];
    $_SESSION['selectedPermit']=$selectedPermit;
    
    foreach($selectedPermit as $permit)
    {
        $query="SELECT t.PREREQ1, t.PREREQ2, (CASE WHEN (t.PREREQ1 IS NOT NULL) AND (p1.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing1, (CASE WHEN (t.PREREQ2 IS NOT NULL) AND (p2.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing2 FROM type AS t LEFT JOIN permit AS p1 ON (t.PREREQ1=p1.ptype) AND ( p1.EID = '$user' ) AND (p1.STATUS='approved') LEFT JOIN permit AS p2 ON (t.PREREQ2=p2.ptype) AND ( p2.EID = '$user' ) AND (p2.STATUS='approved') WHERE t.PTYPE = '$permit' ";
        $result=mysql_query($query,$conn);
        $row=mysql_fetch_assoc($result);
        $missing1=$row['missing1'];
        $missing2=$row['missing2'];
        
        if($missing1=='1' or $missing2=='1')
        {
            $message='You have not met the pre-requisites for '. $permit .' \n';
//              echo "You did have not met the Pre-Requisites for." .$permit;
            echo "<script>alert(\"$message\");";
//              header('Location:s_apply2.php');
            echo "location.href='s_apply2.php';</script>";
        }
        elseif($missing1 =='0' and $missing2 =='0')
        {
            $query="SELECT PTYPE FROM permit WHERE EID='$user'";
            $result=mysql_query($query);
            
            if(mysql_num_rows($result)==0)
            {
                $query="SELECT MED FROM emp WHERE EID='$user'";
                    $result=mysql_query($query);
                    $row=mysql_fetch_assoc($result);
                    $med=$row['MED'];
                
                    if($med == 'yes')
                    {
//                          echo "You are Fine.";
                        $query = "INSERT INTO permit (EID, PTYPE) VALUES ('$user','$permit' )";
                        mysql_query($query);
                        header('Location:s_apply_success.php');
                    }
                    else
                    {
//                          echo "Go do Medical form";
                        header('Location:medical_question.php');
                    }
            }
            else
            {
                while($row=mysql_fetch_assoc($result))
                {
                    $appliedPermit[]=$row['PTYPE'];
                }
            
                if(in_array($permit,$appliedPermit))
                {
                        $message1='You have already applied '. $permit .' before. \n';
//                          echo"You have already applied for ".$permit ." before <br/>";
                        echo "<script>alert(\"$message1\");";
//                          header('Location:s_apply2.php');
                        echo "location.href='s_apply2.php';</script>";
                }
                else
                {
                    $query="SELECT MED FROM emp WHERE EID='$user'";
                    $result=mysql_query($query);
                    $row=mysql_fetch_assoc($result);
                    $med=$row['MED'];
                
                    if($med == 'yes')
                    {
                    //  echo "You are Fine.";
                        $query = "INSERT INTO permit (EID, PTYPE) VALUES ('$user','$permit' )";
                        mysql_query($query);
                        header('Location:s_apply_success.php');
                    }
                    else
                    {
                        echo "Go do Medical form";
                    //  header('Location:medical_question.php');
                    }
                }
            }
        }
    }
    
    dbDisconnect($conn);
  
?>

疑似错误:

在我的代码中,我怀疑是因为我正在使用header('Location:s_apply_success.php');因此导致意外结果发生。但我无法确认这是否是错误。

我为调试可疑错误所做的尝试

但我确实尝试使用echo "You are Fine.";而不是header('Location:s_apply_success.php');并且它有效。它确实起到了回声和弹出的作用。

4

3 回答 3

0

来自 PHP 手册:

http://php.net/manual/en/function.header.php

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。使用 include 或 require 函数或其他文件访问函数读取代码并在调用 header() 之前输出空格或空行是一个非常常见的错误。使用单个 PHP/HTML 文件时也存在同样的问题。

于 2012-04-06T01:13:56.370 回答
0

总而言之,在 header() 之前不能显示或显示任何内容,将消息传递到下一页。

于 2012-04-06T01:15:04.747 回答
0

输出缓冲将在调用之前补偿您的输出回显header()

<?php
session_start();
ob_start();
include'Connections/database.php';
$conn = dbConnect ();
    if (! $conn)
    die("Couldn't connect to MySQL");

    $user = $_SESSION['eid'];
    $selectedPermit=$_POST['cat'];
    $_SESSION['selectedPermit']=$selectedPermit;

    foreach($selectedPermit as $permit)
    {
        $query="SELECT t.PREREQ1, t.PREREQ2, (CASE WHEN (t.PREREQ1 IS NOT NULL) AND (p1.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing1, (CASE WHEN (t.PREREQ2 IS NOT NULL) AND (p2.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing2 FROM type AS t LEFT JOIN permit AS p1 ON (t.PREREQ1=p1.ptype) AND ( p1.EID = '$user' ) AND (p1.STATUS='approved') LEFT JOIN permit AS p2 ON (t.PREREQ2=p2.ptype) AND ( p2.EID = '$user' ) AND (p2.STATUS='approved') WHERE t.PTYPE = '$permit' ";
        $result=mysql_query($query,$conn);
        $row=mysql_fetch_assoc($result);
        $missing1=$row['missing1'];
        $missing2=$row['missing2'];

        if($missing1=='1' or $missing2=='1')
        {
            $message='You have not met the pre-requisites for '. $permit .' \n';
//              echo "You did have not met the Pre-Requisites for." .$permit;
            echo "<script>alert(\"$message\");";
//              header('Location:s_apply2.php');
            echo "location.href='s_apply2.php';</script>";
        }
        elseif($missing1 =='0' and $missing2 =='0')
        {
            $query="SELECT PTYPE FROM permit WHERE EID='$user'";
            $result=mysql_query($query);

            if(mysql_num_rows($result)==0)
            {
                $query="SELECT MED FROM emp WHERE EID='$user'";
                    $result=mysql_query($query);
                    $row=mysql_fetch_assoc($result);
                    $med=$row['MED'];

                    if($med == 'yes')
                    {
//                          echo "You are Fine.";
                        $query = "INSERT INTO permit (EID, PTYPE) VALUES ('$user','$permit' )";
                        mysql_query($query);
                        header('Location:s_apply_success.php');
                    }
                    else
                    {
//                          echo "Go do Medical form";
                        header('Location:medical_question.php');
                    }
            }
            else
            {
                while($row=mysql_fetch_assoc($result))
                {
                    $appliedPermit[]=$row['PTYPE'];
                }

                if(in_array($permit,$appliedPermit))
                {
                        $message1='You have already applied '. $permit .' before. \n';
//                          echo"You have already applied for ".$permit ." before <br/>";
                        echo "<script>alert(\"$message1\");";
//                          header('Location:s_apply2.php');
                        echo "location.href='s_apply2.php';</script>";
                }
                else
                {
                    $query="SELECT MED FROM emp WHERE EID='$user'";
                    $result=mysql_query($query);
                    $row=mysql_fetch_assoc($result);
                    $med=$row['MED'];

                    if($med == 'yes')
                    {
                    //  echo "You are Fine.";
                        $query = "INSERT INTO permit (EID, PTYPE) VALUES ('$user','$permit' )";
                        mysql_query($query);
                        header('Location:s_apply_success.php');
                    }
                    else
                    {
                        echo "Go do Medical form";
                    //  header('Location:medical_question.php');
                    }
                }
            }
        }
    }

    dbDisconnect($conn);
    ob_end_flush();
?>
于 2012-04-06T01:15:41.690 回答