我将多个查询值带到我的前端,并使用一个数组来实现。当我在if语句之前回显我的$hold['POAmount'];
值时,它会按预期返回253{"Sum":10,"POAmount":-10}
,尽管不要将其与期望的输出混淆。但是在输入我的if语句后,返回页面的响应为{"Sum":10,"POAmount":-10}
。我认为也许在fetch_assoc()
上使用fetch_array()
会有所作为,但这无济于事。这是我得到的:
<?php
include '../../inc/dbinfo.inc';
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "################################################# FT-SUM-INVOICES.PHP #################################################" );
$inv = $_POST['inv'];
$status = $_POST['stat'];
//Get PO # From invoice id
$getPO = "SELECT VendorPOID1, VdrInvoiceAmount FROM tblVendorInvoices WHERE VENDORINVOICEID =?";
$exgetPO = $conn->prepare($getPO);
$exgetPO->bind_param("i", $inv);
$exgetPO->execute();
$obj = $exgetPO->get_result();
$hold = $obj->fetch_assoc();
$po = $hold['VendorPOID1'];
$currentInAmount = $hold['VdrInvoiceAmount'];
$cn = 3; //paid status
$getPOAmount = "SELECT POAmount FROM tblVendorPOs WHERE VENDORPOID = ?";
$poAmount = $conn->prepare($getPOAmount);
$poAmount->bind_param("i",$po);
$poAmount->execute();
$obj3 = $poAmount->get_result();
$hold = $obj3->fetch_assoc(); //Total PO Amount
if($status == 3){
echo json_encode($hold['POAmount']);
//Get Sum of invoices on po + current invoice
$qr = "SELECT SUM(VdrInvoiceAmount) AS Sum FROM tblVendorInvoices WHERE VENDORPOID1 = ? AND VdrInvoiceStatusID = ?";
$getSum = $conn->prepare($qr);
$getSum->bind_param("ii", $po, $cn);
$getSum->execute();
$obj2 = $getSum->get_result();
$hold = $obj2->fetch_assoc();
$hold['Sum'] += $currentInAmount;
$hold['POAmount'] -= $hold['Sum']; //remaining balance
if($hold['Sum'] == NULL){
$hold['Sum'] = 0;
}
echo json_encode($hold);
}else{
//Get Sum of invoices on po
$qr = "SELECT SUM(VdrInvoiceAmount) AS Sum FROM tblVendorInvoices WHERE VENDORPOID1 = ? AND VdrInvoiceStatusID = ?";
$getSum = $conn->prepare($qr);
$getSum->bind_param("ii", $po, $cn);
$getSum->execute();
$obj2 = $getSum->get_result();
$hold = $obj2->fetch_assoc();
if($hold['Sum'] == NULL){
$hold['Sum'] = 0;
}
$hold['POAmount'] -= $hold['Sum'];
echo json_encode($hold);
}
?>
我上面描述的if语句是if($status == 3){
,我没有从网页或PHP错误日志中收到任何错误。
我的问题摘要:
当我从中减去总和时, $hold['POAmount']
被认为是null
。因此它以-$hold['Sum']
的形式出现。开头不应该为null。输入我的if语句后,它变成null
。
当前输出:{"Sum":9,"POAmount":-9}
所需的输出:{"Sum":9,"POAmount":243}
答案 0 :(得分:0)
这应该有效(尚未测试)。我更改的是,我已将if语句中的SELECT查询结果分配给另一个名为$ hold2的变量,以使$ hold中的$ hold ['POAmount']值不会被覆盖。>
然后我将$ hold变量中的数组(在if语句之前保存SELECT查询的结果)与$ hold2中的数组(在if语句中保存SELECT查询的结果)合并),生成包含POAmount和Sum键的数组。
让我知道这是否行不通以及您遇到了什么错误,我们很乐意为您提供帮助。 :)
<?php
include '../../inc/dbinfo.inc';
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "################################################# FT-SUM-INVOICES.PHP #################################################" );
$inv = $_POST['inv'];
$status = $_POST['stat'];
//Get PO # From invoice id
$getPO = "SELECT VendorPOID1, VdrInvoiceAmount FROM tblVendorInvoices WHERE VENDORINVOICEID =?";
$exgetPO = $conn->prepare($getPO);
$exgetPO->bind_param("i", $inv);
$exgetPO->execute();
$obj = $exgetPO->get_result();
$hold = $obj->fetch_assoc();
$po = $hold['VendorPOID1'];
$currentInAmount = $hold['VdrInvoiceAmount'];
$cn = 3; //paid status
$getPOAmount = "SELECT POAmount FROM tblVendorPOs WHERE VENDORPOID = ?";
$poAmount = $conn->prepare($getPOAmount);
$poAmount->bind_param("i",$po);
$poAmount->execute();
$obj3 = $poAmount->get_result();
$hold = $obj3->fetch_assoc(); //Total PO Amount
if($status == 3){
echo json_encode($hold['POAmount']);
//Get Sum of invoices on po + current invoice
$qr = "SELECT SUM(VdrInvoiceAmount) AS Sum FROM tblVendorInvoices WHERE VENDORPOID1 = ? AND VdrInvoiceStatusID = ?";
$getSum = $conn->prepare($qr);
$getSum->bind_param("ii", $po, $cn);
$getSum->execute();
$obj2 = $getSum->get_result();
$hold2 = $obj2->fetch_assoc();
$hold = array_merge($hold, $hold2);
$hold['Sum'] += $currentInAmount;
$hold['POAmount'] -= $hold['Sum']; //remaining balance
if($hold['Sum'] == NULL){
$hold['Sum'] = 0;
}
echo json_encode($hold);
}else{
//Get Sum of invoices on po
$qr = "SELECT SUM(VdrInvoiceAmount) AS Sum FROM tblVendorInvoices WHERE VENDORPOID1 = ? AND VdrInvoiceStatusID = ?";
$getSum = $conn->prepare($qr);
$getSum->bind_param("ii", $po, $cn);
$getSum->execute();
$obj2 = $getSum->get_result();
$hold2 = $obj2->fetch_assoc();
$hold = array_merge($hold, $hold2);
if($hold['Sum'] == NULL){
$hold['Sum'] = 0;
}
$hold['POAmount'] -= $hold['Sum'];
echo json_encode($hold);
}
?>