php怎么实现记账功能实现
数据库设计
创建数据库表存储记账数据,通常需要包含以下字段:
id:唯一标识amount:金额type:收支类型(收入/支出)category:分类(餐饮、交通等)date:日期description:备注
CREATE TABLE transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
amount DECIMAL(10,2) NOT NULL,
type ENUM('income', 'expense') NOT NULL,
category VARCHAR(50) NOT NULL,
date DATE NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
表单处理
创建HTML表单用于数据录入:
<form action="add_transaction.php" method="post">
<label>金额: <input type="number" step="0.01" name="amount" required></label>
<label>类型:
<select name="type" required>
<option value="income">收入</option>
<option value="expense">支出</option>
</select>
</label>
<label>分类: <input type="text" name="category" required></label>
<label>日期: <input type="date" name="date" required></label>
<label>备注: <textarea name="description"></textarea></label>
<button type="submit">保存</button>
</form>
数据存储
add_transaction.php处理表单提交:

<?php
$conn = new mysqli('localhost', 'username', 'password', 'accounting_db');
$amount = $_POST['amount'];
$type = $_POST['type'];
$category = $_POST['category'];
$date = $_POST['date'];
$description = $_POST['description'];
$stmt = $conn->prepare("INSERT INTO transactions (amount, type, category, date, description) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("dssss", $amount, $type, $category, $date, $description);
$stmt->execute();
header('Location: index.php');
?>
数据展示
创建页面显示记账记录:
<?php
$conn = new mysqli('localhost', 'username', 'password', 'accounting_db');
$result = $conn->query("SELECT * FROM transactions ORDER BY date DESC");
?>
<table>
<tr>
<th>日期</th>
<th>类型</th>
<th>分类</th>
<th>金额</th>
<th>备注</th>
</tr>
<?php while($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['date'] ?></td>
<td><?= $row['type'] == 'income' ? '收入' : '支出' ?></td>
<td><?= $row['category'] ?></td>
<td><?= $row['amount'] ?></td>
<td><?= $row['description'] ?></td>
</tr>
<?php endwhile; ?>
</table>
统计功能
添加统计功能计算收支情况:

<?php
$income = $conn->query("SELECT SUM(amount) as total FROM transactions WHERE type='income'")->fetch_assoc()['total'];
$expense = $conn->query("SELECT SUM(amount) as total FROM transactions WHERE type='expense'")->fetch_assoc()['total'];
$balance = $income - $expense;
?>
<h3>统计</h3>
<p>总收入: <?= $income ?></p>
<p>总支出: <?= $expense ?></p>
<p>结余: <?= $balance ?></p>
分类统计
按分类统计支出:
<?php
$categories = $conn->query("SELECT category, SUM(amount) as total FROM transactions WHERE type='expense' GROUP BY category");
?>
<h3>支出分类统计</h3>
<ul>
<?php while($row = $categories->fetch_assoc()): ?>
<li><?= $row['category'] ?>: <?= $row['total'] ?></li>
<?php endwhile; ?>
</ul>
安全增强
添加输入验证和防SQL注入措施:
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
$type = in_array($_POST['type'], ['income', 'expense']) ? $_POST['type'] : null;
$category = htmlspecialchars(trim($_POST['category']));
$date = filter_input(INPUT_POST, 'date', FILTER_SANITIZE_STRING);
$description = htmlspecialchars(trim($_POST['description']));
if(!$amount || !$type || !$category || !$date) {
die('无效输入');
}
文件导出
实现导出为CSV功能:
<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="transactions.csv"');
$output = fopen('php://output', 'w');
fputcsv($output, ['日期', '类型', '分类', '金额', '备注']);
$result = $conn->query("SELECT * FROM transactions");
while($row = $result->fetch_assoc()) {
fputcsv($output, [
$row['date'],
$row['type'] == 'income' ? '收入' : '支出',
$row['category'],
$row['amount'],
$row['description']
]);
}
fclose($output);
exit;
?>






