<?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 = "从收据中选择 * 仓库 =$warehouse
有谁知道为什么会这样?
PD: I use this for show the (function 2) data:
<?php
$invoices = getInvoices($warehouse);
if( !empty( $invoices ) ){
foreach ($invoices as $value) {
$value['warehouse'];
}
}
?>
这可能是一个可变范围的问题。 我看不到您的主脚本正在输入什么变量saveInvoice()
,但是如果您要传递一个名为$data
的变量,其中包含code
、data
和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
。 这部分需要更多的开发。