How does this error fix?

I have two files and it has shown two errors in line 72 and 40, I try to fix them it does not fix.
index.php

<?php
// Include the main.php file
require 'main.php';
// Fetch all invoices from the database ordered by the invoice date
$stmt = $pdo->query('SELECT i.*, (SELECT SUM(ii.item_quantity * ii.item_price) FROM invoice_items ii WHERE ii.invoice_id = i.id) AS payment_amount FROM invoices i ORDER BY i.created DESC');
$invoices = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Invoices</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,minimum-scale=1">
        <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
        <div class="invoices">
            <div class="header">
                <h1>Invoices</h1>
                <a href="create.php" class="btn"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" /></svg>Create</a>
            </div>
            <div class="content">
                <table class="invoices-table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Client</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th>Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($invoices as $invoice): ?>
                        <tr>
                            <td class="alt"><?=$invoice['id']?></td>
                            <td><?=$invoice['client_name']?></td>
                            <td><strong><?=$currency_code?><?=number_format($invoice['payment_amount'], 2)?></strong></td>
                            <td>
                                <?php if ($invoice['payment_status'] == 'Paid'): ?>
                                <span class="green">Paid</span>
                                <?php else: ?>
                                <span class="red">Unpaid</span>
                                <?php endif; ?>
                            </td>
                            <td class="alt"><?=date('F d, Y', strtotime($invoice['created']))?></td>
                            <td>
                                <a href="invoice.php?id=<?=$invoice['id']?>" class="action">View</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </body>
</html>

invoice.php

<?php
// Include the main.php file
require 'main.php';
// Check if get ID is set
if (isset($_GET['id'])) {
    // Get the invoice from the database
    // ...
} else {
    // If the ID was not set, output error
    exit('No ID specified!');
}

$stmt = $pdo->prepare('SELECT i.*, (SELECT SUM(ii.item_quantity * ii.item_price) FROM invoice_items ii WHERE ii.invoice_id = i.id) AS payment_amount FROM invoices i WHERE i.id = ?');
$stmt->execute([ $_GET['id'] ]);
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);
// If the invoice does not exist, output error
if (!$invoice) {
    exit('Invoice does not exist!');
}
// Get invoice items
$stmt = $pdo->prepare('SELECT * FROM invoice_items WHERE invoice_id = ?');
$stmt->execute([ $_GET['id'] ]);
$invoice_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Invoices</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,minimum-scale=1">
        <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
        <div class="invoice">
            <div class="header">
                <div>
                    <h1>Invoice</h1>
                    <p class="due-date">Due <?=date('F d, Y', strtotime($invoice['created']))?></p>
                </div>
                <?php if ($invoice['payment_status'] == 'Unpaid'): ?>
                <a href="pay.php?id=<?=$invoice['id']?>" class="btn"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20,8H4V6H20M20,18H4V12H20M20,4H4C2.89,4 2,4.89 2,6V18A2,2 0 0,0 4,20H20A2,2 0 0,0 22,18V6C22,4.89 21.1,4 20,4Z" /></svg>Pay Now</a>
                <?php else: ?>
                <p class="paid"><?=$invoice['payment_status']?></p>
                <?php endif; ?>
            </div>
            <div class="content">
                <div class="from-details">
                    <h3>From</h3>
                    <p><?=nl2br($company_name)?></p>
                    <p><?=nl2br($company_address)?></p>
                </div>
                <div class="to-details">
                    <h3>To</h3>
                    <p><?=$invoice['client_name']?></p>
                    <p><?=nl2br($invoice['client_address'])?></p>
                </div>
                <div class="invoice-items">
                    <table class="invoice-items-table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Quantity</th>
                                <th>Price</th>
                                <th class="pull-right">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($invoice_items as $item): ?>
                            <tr class="item">
                                <td><?=$item['item_name']?></td>
                                <td><?=$item['item_quantity']?></td>
                                <td><?=$currency_code?><?=number_format($item['item_price'], 2)?></td>
                                <td class="pull-right"><?=$currency_code?><?=number_format($item['item_quantity'] * $item['item_price'], 2)?></td>
                            </tr>
                            <?php endforeach; ?>
                            <tr class="total">
                                <td colspan="3" class="pull-right"><strong>Total</strong></td>
                                <td class="pull-right"><?=$currency_code?><?=number_format($invoice['payment_amount'], 2)?></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

thank for your help

I fixed the index.php error in line 40 this way

Can you tell us what the errors say? That’s a big clue.

1 Like

Where does $currency_code come from?

But yes, please tell us what the actual error messages are, and what lines they occur on (mark the lines in the code please, don’t make us count).

Which way?

1 Like

So, looking at your code, the errors on lines 72 and 40 might be happening because of a couple of things. First off, if you’re using variables like $currency_code in your index.php and invoice.php, make sure those are actually defined somewhere before you start using them. If they’re not, PHP’s going to throw an undefined variable error at you.

Also, you’re relying on $pdo for your database queries, which is totally fine, but you need to make sure that your main.php file, which you’re including at the top, is setting up that $pdo connection properly. If there’s any issue with that connection not being established, your queries are going to fail, and that could be part of what’s causing the errors.

One more thing—if any of the data you’re trying to fetch, like client_name or payment_status, is missing from your database or if there’s a typo in your SQL, you might run into problems when you try to display that data.

Honestly, the best way to figure this out is to throw in some var_dump() or echo statements right before the lines where the errors are happening. That’ll give you a clearer idea of what’s going wrong.

If you’re still having trouble, feel free to ask—I’m here to help!

2 Likes

If $pdo was undefined or false, the error would generate on line 5 of index.php, not line 40. The fact that we got to line 40 tells you that this isnt the case. Presumably, the mystery error is that $currency_code does not exist (It’s the commonality between the two lines), but if not, both $invoice['payment_amount'] and $invoice['item_price'] don’t exist.