IF语句未在php中验证

时间:2013-03-11 21:21:48

标签: php if-statement

我在一个页面上有这个PHP脚本,它从输入字段中获取变量:

if($fname <> "" and $lname <> "" and $ydept <> "") {
    if ($percentage >= "85") {
        mail ($myEmail, $mailSubject, $msgBody, $header);
        mail ($userEmail, $sentMailSubject, $sentMailBody, $sentHeader);
        $filename = "blah.txt"; #Must CHMOD to 666, set folder to 777
        $text = "\n" . str_pad($fname, 25) . "" . str_pad($lname, 25) . "" . str_pad($empID, 15) . "" . str_pad($doh, 15) . "";

        $fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
        if ($fp) {
            fwrite ($fp, $text);
            fclose ($fp);
            #echo ("File written");
        }
        else {
            #echo ("File was not written");
        }
        header ("Location: yes.php?fname=$fname&type=$type");
    }
    else {
        header ("Location: didnotpass.php?percent=$percentage&type=$type");
    }
}
else {
    header ("Location: empty.php?type=$type");
}

didnotpass.php脚本是:

<?php
if (isset($_GET['type']) && isset($_GET['percentage'])) {
    $percent = trim(strip_tags(stripslashes($_GET['percent'])));
    $type = trim(strip_tags(stripslashes($_GET['type'])));

    if ($type == "Clinical") {
?>
The Certificate of Attendance has been completed yet, but your score of $percent% is less then the passing score of 85%.<br><br>
Please <b><a href="C.php">Click Here</a></b> to retake the certification.
<?php
    }
    else {
?>
The Certificate of Attendance has been completed yet, but your score of $percent% is less then the passing score of 85%.<br><br>
Please <b><a href="nonC.php">Click Here</a></b> to retake the certification.
<?php
    }
}
else {
?>
Please <b><a href="start.php">Go To Certification Homepage</a></b> to start the certification.
<?php
}
?>

我遇到的问题是得分是否为85%,这是最后一句话:

Please <b><a href="start.php">Go To Certification Homepage</a></b> to start the certification.

我的IF声明是否正确:

if ($percentage >= "85")

我认为它正常工作,因为它正在路由到正确的页面,但didnotpass.php中的IF / ELSE语句无法正常工作。有人可以帮我解决吗?

2 个答案:

答案 0 :(得分:3)

if ($percentage >= "85")

应该是:

if ($percentage >= 85)

不能在数字周围加引号,然后期望它评估为&gt; =。它会将其视为一个字符串。您可能需要将其解析为整数,具体取决于它的类型。

答案 1 :(得分:2)

我认为发生的事情是percent的查询字符串参数不一致:

if (isset($_GET['type']) && isset($_GET['percentage'])) {
    $percent = trim(strip_tags(stripslashes($_GET['percent'])));
    $type = trim(strip_tags(stripslashes($_GET['type'])));

应该是

if (isset($_GET['type']) && isset($_GET['percent'])) {
    $percent = trim(strip_tags(stripslashes($_GET['percent'])));
    $type = trim(strip_tags(stripslashes($_GET['type'])));

if语句引用$_GET['percentage']而不是$_GET['percent']