2013-03-11 90 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