doushijiao0679 2018-08-09 20:49
浏览 40
已采纳

为什么输入if语句时我的数组值会消失?

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}

  • 写回答

1条回答 默认 最新

  • dongman2721 2018-08-09 21:20
    关注

    This should work (haven't tested it). What I've changed is, I've assigned the result of the SELECT query within the if statement to a different variable named $hold2, so that the $hold['POAmount'] value in $hold isn't overwritten.

    And then I've merged the array in the $hold variable (which holds the result from the SELECT query right before the if statement) with the array in $hold2 (which holds the result from the SELECT query inside the if statement), which produces an array with both the POAmount and Sum keys in it.

    Let me know if this isn't working and what errors you're getting, I'd be happy to help. :)

    <?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);
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了