2013-03-11 80 views
0

验证我有一个页面上此PHP脚本这需要从变量输入字段:IF语句不能在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语句correc T:

if ($percentage >= "85") 

我想这是正常工作,因为它被路由到正确的页面,但在didnotpass.php的if/else语句无法正常工作。任何人都可以帮我修复它?

+0

这似乎很容易检查:在'if'条件给你之前'var_dump($ percent);'是什么,你为什么要引用nu MBER? – jeroen 2013-03-11 21:24:06

+0

我的nopass.php网址如下所示:http://www.imed.com/sc/nopass.php?percent=38&type=Clinical – Si8 2013-03-11 21:28:43

+0

你知道,这并不是说这可以解决你的问题,但它可能是一个坏主意在数字周围使用引号。只要做'85'不加引号。 – Mike 2014-04-03 00:52:53

回答

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']

+0

,我知道我错过了某个地方!谢谢JLC。问题修复了! – Si8 2013-03-11 21:33:56

3
if ($percentage >= "85") 

应该是:

if ($percentage >= 85) 

不能围绕着一些报价,然后指望它评估为> =。它会把它当作一个字符串。您可能需要将其解析为一个整数,具体取决于它是什么类型。

+0

谢谢。你的建议只是将比较字符串固定为整数,但JLC的固定IF语句问题的比例为 – Si8 2013-03-11 21:33:24