2013-09-26 144 views
-1

似乎最後一段越來越難了!這段代碼爲什麼執行?

所以我有if語句和內部這些語句代碼應該執行,如果他們是真的。問題是代碼正在執行,即使它不是真的。雖然Spproved設置爲1

if($user_data['permissions'] >= 1) 
{ 
    // If users permission is 1 or 2 they get a field for inputting the index # and a button to change the approve field from 0 to 1 may need to make a new field to record who approved it.... 

    //Determine if the order is already approved. If not approved show index field and allow user to approve it with index number 
    if($data2[0]['Approved'] == 1) 
    { 
     echo " <font color=\"green\"> Approved"; 
    } 
    else if($data2[0]['Approved'] == 0) 
    { 
     echo " Not Approved. Supply an index number and click approve to authorize this order to be completed."; 

     if (empty ($_GET) === false) 
     { 
      $required_fields = array('IndexNum'); 
      foreach ($_GET as $key=>$value) 
      { 
       if (empty($value) && in_array($key, $required_fields) === true) 
       { 
       $errors[] = 'Fields marked with an asterisk are required'; 
       break 1; 
       } 
      } 

      if (isset($_GET['success']) === true && empty($_GET['success']) === true) 
      {    
       echo 'Index has been updated and Order is now set to Approved'; 
      } 
      else 
      {  
       if (empty($errors) === true) 
       { 
        $indexnum=$_GET['IndexNum']; 
        $approvedby=$user_data['lname']; 
        $vendorid1= $_GET['hidden1']; 

        update_approved($approvedby, $indexnum, $vendorid1); 
        header('Location: index.php'); 
        exit();   
       } 
       else if(empty($errors) === false) 
       { 
        echo output_errors($errors); 
       } 
      } 
     } 

     ?>    
     <form name="approveform" method="GET" action=""> 
     <input type="hidden" name="hidden1" value="<?php echo $id;?>">" 
     Index Number*: <input type="text" name="IndexNum">&nbsp; 
     <input type="submit" value="Approve" action=""> 
     </form> 

<?php }  
} 

if($user_data['permissions'] == 2) 
{ 
    // If user is permission 2 they can have a button to say shipped... Do I need to record who shipped it? for now nah. Would be nice to input a data of arrival though. I will think on it .... pretty lazy 
    if($data2[0]['Approved'] == 1) 
    { 
    echo "<br/>"; 
    echo "Confirm order has been ordered"; 

    if(isset($_GET['Ordered'])) 
    { 
     $vendorid1=$_GET['hidden1']; 

     echo $vendorid1; 
     //update_shipped($vendorid1); 
     //header('Location: index.php'); 
     //exit(); 
    }  
    ?> 

    <form name="approveform" method="GET" action=""> 
    <input type="hidden" name="hidden1" value="<?php echo $id;?>"> 
    <input type="submit" name="Ordered" value="Ordered" action=""> 
    </form> 

    <?php    
    }  
}  

它顯示爲綠色批准的格式上與點按鈕上來罰款執行甚至

代碼。當我點擊提交按鈕時,else if($ data [0] ['Approved'] == 0)中的代碼會激活而不是代碼isset。批准設置爲1,所以我不知道爲什麼該代碼運行.....

價值print_r($data2)

Array ([0] => Array ([VendorName] => Newegg [DateRequested] => 2013-09-25 
[DateNeeded] => 0000-00-00 [Shipping] => Standard [VendorNumber] => 123123 
[VendorFax] => NA [VendorAddress] => 1 ave new [VendorCity] => socorro 
[VendorState] => nm [VendorZip] => 87114 [EquipmentConsumable] => Consumable 
[GasType] => propane [GasLocation] => United States [UNMTag] => 0 
[EquipmentLocation] => [index] => 414141 [totalcost] => 129.88 
[Approved] => 1 [Shipped] => 0)) 
+1

顯示print_r的輸出($數據2)。 –

+0

數組([0] =>數組([VendorName] => Newegg [DateRequested] => 2013-09-25 [DateNeeded] =>標準[VendorNumber] => 123123 [VendorFax ] =>不適用[VendorAddress] => 1 ave new [VendorCity] => socorro [VendorState] => nm [VendorZip] => 87114 [EquipmentConsumable] =>消耗品[GasType] =>丙烷[GasLocation] => UNMTag] => 0 [EquipmentLocation] => [index] => 414141 [totalcost] => 129.88 [Approved] => 1 [Shipped] => 0)) – HELPMEPLEASE

+0

你有什麼是邏輯錯誤。通過你的代碼調試每個條件和任務,你會得到你的答案。沒有理由讓別人爲你做。 –

回答

1

使用嚴格的比較,===!==代替==!=。除非另有說明,PHP往往會將1和0評估爲布爾值。

此外,與像empty()功能,您可以更改:

if(empty($_GET) === FALSE)if(!empty($_GET))
if(empty($_GET) === TRUE)if(empty($_GET))

當他們返回boolean值。

由於您正在使用$ _GET,請確保您每次傳遞所需的所有變量都在url中。由於表單元素只能傳遞嵌套的輸入元素,因此提交後可能需要傳遞更多隱藏信息。另外,您應該將文件名稱放在action=""中,或者從表單標記中省略它。

+0

或只是(空($ _ get)) – Snowburnt

+0

雖然這固定它,但似乎並沒有。所以它對===做的是顯示正確的信息,但現在如果我使用提交按鈕,它不處理信息只是返回頁面。 現在,在添加===之前,Approveform用於這兩個函數。現在這兩種形式都只是在提交時進入空白頁面。 – HELPMEPLEASE

+0

嗯,它縮小了。 @Snowburnt我其實是在談論使用整數的第一個條件。 – Jason

1

我知道這不會解決你的問題,但是....

              You have an extra " 
                  right here 
                    | 
                    V 
    <input type="hidden" name="hidden1" value="<?php echo $id;?>">" 
    Index Number*: <input type="text" name="IndexNum">&nbsp; 
    <input type="submit" value="Approve" action=""> 
+0

哎呀謝謝!沒有看到。 – HELPMEPLEASE