-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduct_import_export.php.bak
152 lines (138 loc) · 6.62 KB
/
product_import_export.php.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
session_start();
require_once 'config.php';
require_once 'auth.php';
requireLogin();
$user_id = getCurrentUserId();
$message = '';
// 處理導出請求
if (isset($_POST['export'])) {
$stmt = $conn->prepare("SELECT name, barcode, stock, price, purchase_price FROM products WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$filename = "products_export_" . date('Y-m-d') . ".csv";
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// 輸出 UTF-8 BOM
echo "\xEF\xBB\xBF";
$output = fopen('php://output', 'w');
fputcsv($output, array('商品名稱', '條碼', '庫存', '售價', '採購價'));
while ($row = $result->fetch_assoc()) {
// 確保所有數據都是 UTF-8 編碼,無需額外轉換
fputcsv($output, $row);
}
fclose($output);
exit();
}
// 處理導入請求
if (isset($_POST['import'])) {
if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] == 0) {
$file = $_FILES['csv_file']['tmp_name'];
if (($handle = fopen($file, "r")) !== FALSE) {
// 檢測並移除 UTF-8 BOM
$bom = fread($handle, 3);
if ($bom != "\xEF\xBB\xBF") {
rewind($handle);
}
// 跳過標題行
fgetcsv($handle);
$conn->begin_transaction();
try {
$stmt = $conn->prepare("INSERT INTO products (name, barcode, stock, price, purchase_price, user_id) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE stock = VALUES(stock), price = VALUES(price), purchase_price = VALUES(purchase_price)");
$count = 0;
while (($data = fgetcsv($handle)) !== FALSE) {
// 假設 CSV 文件已經是 UTF-8 編碼,無需額外轉換
$stmt->bind_param("ssiddi", $data[0], $data[1], $data[2], $data[3], $data[4], $user_id);
$stmt->execute();
$count++;
}
$conn->commit();
$message = "成功導入 {$count} 條商品記錄。";
} catch (Exception $e) {
$conn->rollback();
$message = "導入失敗: " . $e->getMessage();
}
fclose($handle);
} else {
$message = "無法打開文件。";
}
} else {
$message = "請選擇一個CSV文件。";
}
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商品數據導入/導出 - 庫存管理系統</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<nav class="bg-white shadow-lg">
<div class="max-w-6xl mx-auto px-4">
<div class="flex justify-between">
<div class="flex space-x-7">
<div>
<a href="index.php" class="flex items-center py-4 px-2">
<span class="font-semibold text-gray-500 text-lg">庫存管理系統</span>
</a>
</div>
</div>
<div class="hidden md:flex items-center space-x-3">
<a href="product_list.php" class="py-2 px-2 font-medium text-gray-500 rounded hover:bg-green-500 hover:text-white transition duration-300">商品列表</a>
<a href="inventory_operation.php" class="py-2 px-2 font-medium text-gray-500 rounded hover:bg-green-500 hover:text-white transition duration-300">入庫/出庫</a>
<a href="product_import_export.php" class="py-2 px-2 font-medium text-gray-500 rounded hover:bg-green-500 hover:text-white transition duration-300">數據導入/導出</a>
<a href="my_account.php" class="py-2 px-2 font-medium text-gray-500 rounded hover:bg-green-500 hover:text-white transition duration-300">我的帳號</a>
<a href="logout.php" class="py-2 px-2 font-medium text-gray-500 rounded hover:bg-red-500 hover:text-white transition duration-300">登出</a>
</div>
</div>
</div>
</nav>
<div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-4">商品數據導入/導出</h1>
<?php if (!empty($message)): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo $message; ?></span>
</div>
<?php endif; ?>
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<h2 class="text-2xl font-bold mb-4">導出商品數據</h2>
<form method="POST">
<button type="submit" name="export" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
導出CSV
</button>
</form>
</div>
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<h2 class="text-2xl font-bold mb-4">導入商品數據</h2>
<form method="POST" enctype="multipart/form-data">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="csv_file">
選擇CSV文件
</label>
<input type="file" name="csv_file" id="csv_file" accept=".csv" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<button type="submit" name="import" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
導入CSV
</button>
</form>
</div>
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<h2 class="text-2xl font-bold mb-4">CSV格式說明</h2>
<p class="mb-2">CSV文件應包含以下列:</p>
<ul class="list-disc list-inside mb-4">
<li>商品名稱</li>
<li>條碼</li>
<li>庫存</li>
<li>售價</li>
<li>採購價</li>
</ul>
<p>第一行應為列標題。數據從第二行開始。請確保CSV文件使用UTF-8編碼。</p>
</div>
</div>
</body>
</html>