You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PDO与TCPDF从MySQL按ID打印指定客户发票?

Hey there! Let's figure out how to get that print button working for specific invoice IDs. Since you're new to PHP, I'll break this down step by step with code examples that fit your existing setup.

1. Add Invoice ID to Your Print Button

First, in the loop where you display your invoices, modify the print button to pass the unique invoice ID as a parameter. Here's how to integrate it with your existing code flow:

// Assume this is part of your existing invoice display loop
while($row = mysqli_fetch_assoc($result)) {
    echo "<div class='invoice-entry'>";
    echo "<h3>发票 ID: " . $row['id'] . "</h3>";
    echo "<p>客户名称: " . $row['customer_name'] . "</p>";
    // Add the print button with the invoice ID attached
    echo "<a href='print_single_invoice.php?id=" . $row['id'] . "' target='_blank' class='print-btn'>打印此发票</a>";
    echo "</div>";
}
  • target='_blank' opens the print page in a new tab, which is better for user experience
  • print_single_invoice.php is a new file we'll create next to handle the print-specific logic
2. Create the Print-Specific Page (print_single_invoice.php)

This page will fetch the specific invoice using the ID passed from the button, then render a clean, print-friendly version:

<?php
include 'db.php'; // Reuse your existing database connection file

// Validate and get the invoice ID from the URL
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    $invoice_id = $_GET['id'];
    // Use prepared statements to prevent SQL injection (critical for security!)
    $stmt = $conn->prepare("SELECT * FROM invoices WHERE id = ?");
    $stmt->bind_param("i", $invoice_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $invoice = $result->fetch_assoc();

    if(!$invoice) {
        echo "<p>抱歉,找不到对应的发票!</p>";
        exit;
    }
} else {
    echo "<p>无效的发票 ID,请重试!</p>";
    exit;
}
?>
<!DOCTYPE html>
<html dir="rtl">
<head>
    <title>打印发票 #<?php echo $invoice['id']; ?></title>
    <!-- Print-only styles to optimize layout -->
    <style>
        @media print {
            body { font-family: Arial, sans-serif; line-height: 1.5; }
            .no-print { display: none; }
            .invoice-wrapper { width: 90%; max-width: 800px; margin: 0 auto; padding: 20px; border: 1px solid #ddd; }
            .invoice-header { text-align: center; margin-bottom: 30px; border-bottom: 1px solid #ddd; padding-bottom: 10px; }
            .invoice-details p { margin: 6px 0; }
        }
        .print-btn {
            padding: 8px 16px; background: #28a745; color: white; text-decoration: none; border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="invoice-wrapper">
        <div class="invoice-header">
            <h2>客户发票</h2>
            <p>发票编号: #<?php echo $invoice['id']; ?></p>
        </div>
        <div class="invoice-details">
            <p><strong>客户名称:</strong> <?php echo $invoice['customer_name']; ?></p>
            <p><strong>发票日期:</strong> <?php echo $invoice['invoice_date']; ?></p>
            <p><strong>总金额:</strong> <?php echo $invoice['total_amount']; ?> 元</p>
            <!-- Add more fields from your database here (e.g., item details, payment status) -->
        </div>
        <button class="no-print print-btn" onclick="window.print()">打印发票</button>
    </div>
</body>
</html>

Key notes here:

  • Prepared statements are used to avoid SQL injection—this is a must-have security habit even for beginners
  • The @media print styles ensure only the invoice content is printed (the button gets hidden)
  • Adjust the database fields (like customer_name, invoice_date) to match your actual table structure
3. Quick Checks for Your Existing Code
  • Make sure your invoice table's primary key is named id—if it's something else (like invoice_id), update the code accordingly
  • Double-check that db.php is correctly connecting to your database (no typos in credentials)
  • If your invoices have line items (e.g., purchased products), add an extra query in print_single_invoice.php to fetch and display those details
4. Test It Out!
  1. Load your main invoice list page
  2. Click the print button for any invoice—it'll open a new tab with that specific invoice
  3. Use the browser's print function (Ctrl+P / Command+P) or the on-page button to print the invoice

内容的提问来源于stack exchange,提问作者PHP NOOB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:42:17