Lara Ch Lara Ch - 6 months ago 20
PHP Question

Error of undefined variable in PHP

I have this PHP and MySQL script to know how much we have debts and the total paid debts:

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);


The
$arr
is returned into success function of AJAX call.

But what I see is an error:


Notice: Undefined index: paid in
C:\wamp\www\dentist\pages\get_total_debts.php on line 40


And I am seeing the result of $inst:

enter image description here

I tried to get the
$paid
inside the foreach loop but still getting the same error.

Answer

It should be $paid = $paid + $r['Paid']; (Capital P in Paid)