I'm bringing multiple queried values to my front end and doing so with an array. When I echo my value of $hold['POAmount'];
before my if statement it returns as expected 253{"Sum":10,"POAmount":-10}
although don't confuse this with my desired output. But after entering my if statement the response back to the page is {"Sum":10,"POAmount":-10}
. I thought maybe using fetch_assoc()
over fetch_array()
would do something but this does nothing. Heres what I got:
<?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);
}
?>
My if statement I am describing above is if($status == 3){
I am not receiving any errors from the web page or my PHP error log.
Summary of my problem:
$hold['POAmount']
is considered null
when I subtract sum from it. So it is coming up as -$hold['Sum']
. Whereas it shouldn't be null to begin with. It is becoming null
upon entering my if statement.
Current Output: {"Sum":9,"POAmount":-9}
Desired Output: {"Sum":9,"POAmount":243}