2013-09-26 145 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