0

我在一页上有这个 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%,它转到最后一个 else 语句:

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

我的 IF 语句是否正确:

if ($percentage >= "85")

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

4

2 回答 2

3
if ($percentage >= "85")

应该:

if ($percentage >= 85)

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

于 2013-03-11T21:24:53.123 回答
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']

于 2013-03-11T21:27:44.663 回答