要使用PHP操作發票(viewinvoice),您可以遵循以下步驟:
確保您的服務器已安裝PHP和相關的數據庫(例如MySQL)。
創建一個名為invoices
的數據庫表,用于存儲發票信息。以下是一個簡單的示例SQL語句,用于創建該表:
CREATE TABLE invoices (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
invoice_number VARCHAR(255) NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
due_date DATE NOT NULL,
status VARCHAR(50) NOT NULL
);
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// 創建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
?>
<?php
// 查詢所有發票
$sql = "SELECT * FROM invoices";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 輸出每行數據
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - 客戶名: " . $row["customer_name"]. " - 發票號: " . $row["invoice_number"]. " - 金額: " . $row["amount"]. " - 到期日期: " . $row["due_date"]. " - 狀態: " . $row["status"]. "<br>";
}
} else {
echo "0 結果";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// 創建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
// 插入新發票記錄
$customer_name = "張三";
$invoice_number = "20210601001";
$amount = 100.00;
$due_date = "2021-06-30";
$status = "已支付";
$sql = "INSERT INTO invoices (customer_name, invoice_number, amount, due_date, status)
VALUES ('$customer_name', '$invoice_number', '$amount', '$due_date', '$status')";
if ($conn->query($sql) === TRUE) {
echo "新記錄插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
請注意,這只是一個簡單的示例,實際應用中可能需要考慮更多的因素,例如數據驗證、錯誤處理和安全性。希望這些信息對您有所幫助!