对两个PHP函数使用相同的变量

时间:2017-07-26 01:30:31

标签: php arrays function mysqli global-variables

<?php require_once 'config.php';

// FUNCTION 1 BEGINS
        function saveInvoice( array $data){         if( !empty( $data ) ){              global $con;            $count = 0;             if( isset($data['data'] )){
                        foreach ($data['data'] as $value) {
                            if(!empty($value['length'] ))$count++;
                        }           }           if($count == 0)throw new Exception( "Please add atleast one item to warehouse form." );
         if( !empty( $data)){
                        $codigo1 = mysqli_real_escape_string( $con, trim( $data['codigo1'] ) );
                        $shipper = mysqli_real_escape_string( $con, trim( $data['shipper'] ) );
                        $codigo2 = mysqli_real_escape_string( $con, trim( $data['codigo2'] ) );
                        $consignee = mysqli_real_escape_string( $con, trim( $data['consignee'] ) );
                        $carrier = mysqli_real_escape_string( $con, trim( $data['carrier'] ) );
                        $supplier = mysqli_real_escape_string( $con, trim( $data['supplier'] ) );
                        $tracking = mysqli_real_escape_string( $con, trim( $data['tracking'] ) );
                        $vlb_total = mysqli_real_escape_string( $con, trim( $data['airtotal'] ) );
                        $ft3_total = mysqli_real_escape_string( $con, trim( $data['oceantotal'] ) );
                        $weight_total = mysqli_real_escape_string( $con, trim( $data['totalweight'] ) );
                        $method = mysqli_real_escape_string( $con, trim( $data['method'] ) );
                        $user = mysqli_real_escape_string( $con, trim( $data['user'] ) );
                        $office = mysqli_real_escape_string( $con, trim( $data['office'] ) );
                        $notes = mysqli_real_escape_string( $con, trim( $data['notes'] ) );
                        $total_air_usd = mysqli_real_escape_string( $con, trim( $data['totalairusd'] ) );
                        $total_ocean_usd = mysqli_real_escape_string( $con, trim( $data['totaloceanusd'] ) );

                        $warehouse = mysqli_real_escape_string( $con, trim( $data['warehouse'] ) );

                        if(empty($warehouse)){
                            $uuid = uniqid();
                        $query = "INSERT INTO wreceipt (`warehouse`, `codigo1`, `shipper`, `codigo2`, `consignee`, `carrier`, `supplier`, `tracking`, `vlb_total`, `ft3_total`, `weight_total`, `method`, `user`, `office`, `notes`, `total_air_usd`, `total_ocean_usd`, `created`, `uuid`) VALUES ('', '$codigo1', '$shipper', '$codigo2', '$consignee', '$carrier', '$supplier', '$tracking', '$vlb_total', '$ft3_total', '$weight_total', '$method', '$user', '$office', '$notes', '$total_air_usd', '$total_ocean_usd', NOW() + INTERVAL 1 HOUR, '$uuid')";
                        }else{
                            throw new Exception( "Please check, some of the required fields missing" );
                        }
                        if(!mysqli_query($con, $query)){
                            throw new Exception(  mysqli_error($con) );
                        }else{
                        if(empty($warehouse))$warehouse = mysqli_insert_id($con);
                        }

                        if( isset( $data['data']) && !empty( $data['data'] )){
                            saveInvoiceDetail( $data['data'], $warehouse );
                        }
                        return [
                            'success' => true,
                            'uuid' => $uuid,
                            'message' => 'Warehouse Saved Successfully.',
                            'warehouse' => $warehouse
                        ];          }else{
                        throw new Exception( "Please check, some of the required fields missing" );             }       } else{             throw new Exception( "Please check, some of the required fields missing" );         }   }
// FUNCTION 1 ENDS

// FUNCTION 2 BEGINS       
        function saveInvoiceDetail(array $wreceipt_items, $warehouse = ''){     global $con;

            foreach ($wreceipt_items as $wreceipt_item){
                $desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
                $length = mysqli_real_escape_string( $con, trim( $wreceipt_item['length'] ) );
                $width = mysqli_real_escape_string( $con, trim( $wreceipt_item['width'] ) );
                $height = mysqli_real_escape_string( $con, trim( $wreceipt_item['height'] ) );
                $weight = mysqli_real_escape_string( $con, trim( $wreceipt_item['weight'] ) );
                $quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );
                $volumeweight = mysqli_real_escape_string( $con, trim( $wreceipt_item['volumeweight'] ) );
                $volume = mysqli_real_escape_string( $con, trim( $wreceipt_item['volume'] ) );
                $weightrow = mysqli_real_escape_string( $con, trim( $wreceipt_item['weightrow'] ) );

                $query = "INSERT INTO wreceipt_items (`id`, `warehouse`, `desc`, `length`, `width`, `height`, `weight`, `quantity`, `volumeweight`, `volume`, `weightrow`)
                        VALUES (NULL, '$warehouse', '$desc', '$length', '$width', '$height', '$weight', '$quantity', '$volumeweight', '$volume', '$weightrow' )";
                mysqli_query($con, $query);
            } }

        function getInvoices($warehouse){
                global $con;
                $data = [];
                $query = "SELECT * FROM wreceipt WHERE warehouse = '$warehouse'";
                if ( $result = mysqli_query($con, $query) ){
                    while($row = mysqli_fetch_assoc($result)) {
                        array_push($data, $row);
                    }
                }    return $data; } ?>
// FUNCTION 2 ENDS

我有两个功能:一个寄存器,另一个显示数据。我对第二个函数感兴趣,因为它没有采用 $ warehouse 变量(第一个函数)的值来在第二个函数的查询中使用它正好在这里:$ query =&#34; SELECT * FROM wreceipt WHERE warehouse = $ warehouse

有没有人知道为什么会这样?

PD: I use this for show the (function 2) data:

<?php 
$invoices = getInvoices($warehouse);

if( !empty( $invoices ) ){ 
foreach ($invoices as $value) {
$value['warehouse'];
}
}
?>

1 个答案:

答案 0 :(得分:0)

这可能是变量范围的问题。我无法看到从主脚本提供的变量saveInvoice(),但是如果要传递一个名为$data的变量,其中包含codedata等密钥, warehouse然后在您的主文中使用getinvoices()代替$invoices = getInvoices($warehouse);

的同一数组

再次讨论函数内部的变量范围......

function saveInvoice( array $data){ ... getInvoices($warehouse);内部有效,因为$warehouse是一个声明的变量,在函数内部有一个值。

saveInvoice()之外,变量$warehouse不存在(除非您在未发布到此问题的代码中将其声明为全局范围)。

出于这个原因,我认为解决方案是(在您的第二个发布的代码块中)调用getInvoices()这样的内容:$invoices = getInvoices($data['warehouse']);

此外,我建议您不要返回mysqli转义值。我的建议是在saveInvoiceDetail()和所有其他数据库查询函数中使用mysqli预处理语句。

最后,仔细查看包含success的数组。即使没有成功,它也会返回success=>true。这部分需要更多的发展。