PHP中未定义变量的错误

时间:2016-05-16 06:13:42

标签: php mysql

我有这个PHP和MySQL脚本来了解我们有多少债务和已支付的债务总额:

header('Content-Type: application/json');
$arr[] = 0;
$paid = 0;
$inst = 0;
$sql = "SELECT patient_id, sum(project_cost) as 'cost' FROM patient_info WHERE payment_type = :pt GROUP BY patient_id";

$stmt = $conn->prepare($sql);
$stmt->bindValue(':pt', "installment");
$stmt->execute();

$res = $stmt->fetchAll();
//$count = $stmt->rowCount();
foreach ($res as $row) 
{
    $sql2 = "SELECT sum(payment) as 'Paid' FROM debt
             WHERE  patient_id = :pid";
    $stmt2 = $conn->prepare($sql2);
    $stmt2->bindValue(':pid', $row['patient_id']);
    $stmt2->execute();

    $res2 = $stmt2->fetchAll();
    foreach($res2 as $r)
    {

        if($row['cost'] != $r['Paid'])
        {
            $paid = $paid + $r['paid']; 
        }
    }
    $inst = $inst + $row['cost'];
}

$arr = array("inst" => $inst, "paid" => $paid);

echo json_encode($arr);

$arr返回到AJAX调用的成功函数。

但我看到的是一个错误:

  

注意:未定义的索引:已付款   第40行的C:\ wamp \ www \ dentist \ pages \ get_total_debts.php

我看到了$ inst的结果:

enter image description here

我试图在foreach循环中获取$paid但仍然得到相同的错误。

3 个答案:

答案 0 :(得分:3)

它应该是$paid = $paid + $r['Paid'];(付费的资本P)

答案 1 :(得分:1)

您应该更改以下代码

$paid = $paid + $r['paid']; 

$paid = $paid + $r['Paid']; 

PHP区分大小写

答案 2 :(得分:0)

if($row['cost'] != $r['Paid'])
{
    $paid = $paid + $r['paid']; 
}

我注意到第一个$r['Paid']有一个大写P。也许这就是问题?