Lara Ch Lara Ch - 2 years ago 111
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");

$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']);

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

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
inside the foreach loop but still getting the same error.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download