-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_import.php
168 lines (141 loc) · 6.75 KB
/
sql_import.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
session_start();
require 'db.php';
// Check if user is admin
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'admin') {
header("Location: index.php");
exit;
}
// Function to import data from a CSV or an XLSX file
function importData($filePath)
{
global $conn;
// Use finfo to get the MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $filePath);
finfo_close($finfo);
echo "<div class='alert alert-info'>Detected MIME type: " . htmlspecialchars($mimeType) . "</div>";
if ($mimeType === 'text/plain' || $mimeType === 'text/csv') {
// Read CSV file
$file = fopen($filePath, 'r');
if ($file === false) {
die("<div class='alert alert-danger'>Error opening CSV file.</div>");
}
$header = fgetcsv($file); // Get the first row as header
if ($header === false) {
die("<div class='alert alert-danger'>Error reading CSV header.</div>");
}
// Trim whitespace and convert to lowercase for matching
$header = array_map('trim', $header);
$header = array_map('strtolower', $header);
echo "<div class='alert alert-info'>CSV header detected: " . htmlspecialchars(implode(", ", $header)) . "</div>";
// Read rows
while (($row = fgetcsv($file)) !== false) {
echo "<div class='alert alert-info'>Read row: " . htmlspecialchars(implode(", ", $row)) . "</div>"; // Output the row read
processRow($row, $header);
}
fclose($file);
} elseif ($mimeType === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
// Handle XLSX files here...
// (Code for handling XLSX files will be similar to CSV handling)
} else {
die("<div class='alert alert-danger'>Unsupported file type: " . htmlspecialchars($mimeType) . "</div>");
}
}
// Function to process each row
function processRow($row, $header)
{
global $conn;
// Map headers to indices (using lowercase for matching)
$nameIndex = array_search('name', $header);
$descriptionIndex = array_search('description', $header);
$buyingPriceIndex = array_search('buying price', $header);
$sellingPriceIndex = array_search('selling price', $header);
$quantityIndex = array_search('quantity', $header);
$categoryIndex = array_search('category', $header);
$accountsIndex = array_search('accounts', $header);
$vendorIndex = array_search('vendor', $header);
// Check if all necessary indices are found
if ($nameIndex === false || $descriptionIndex === false ||
$buyingPriceIndex === false || $sellingPriceIndex === false ||
$quantityIndex === false || $categoryIndex === false ||
$accountsIndex === false || $vendorIndex === false) {
echo "<div class='alert alert-danger'>One or more required headers are missing.</div>";
echo "Header mapping:<br>";
echo "Name Index: $nameIndex<br>";
echo "Description Index: $descriptionIndex<br>";
echo "Buying Price Index: $buyingPriceIndex<br>";
echo "Selling Price Index: $sellingPriceIndex<br>";
echo "Quantity Index: $quantityIndex<br>";
echo "Category Index: $categoryIndex<br>";
echo "Accounts Index: $accountsIndex<br>";
echo "Vendor Index: $vendorIndex<br>";
return;
}
// Extract data
$name = isset($row[$nameIndex]) ? $row[$nameIndex] : null;
$description = isset($row[$descriptionIndex]) ? $row[$descriptionIndex] : null;
$buyingPrice = isset($row[$buyingPriceIndex]) ? $row[$buyingPriceIndex] : null;
$sellingPrice = isset($row[$sellingPriceIndex]) ? $row[$sellingPriceIndex] : null;
$quantity = isset($row[$quantityIndex]) ? $row[$quantityIndex] : null;
$category = isset($row[$categoryIndex]) ? $row[$categoryIndex] : null;
$account = isset($row[$accountsIndex]) ? $row[$accountsIndex] : null;
$vendor = isset($row[$vendorIndex]) ? $row[$vendorIndex] : null;
// Show values to be inserted
echo "<div class='alert alert-info'>Values to insert: Name: $name, Description: $description, Buying Price: $buyingPrice, Selling Price: $sellingPrice, Quantity: $quantity, Category: $category, Account: $account, Vendor: $vendor</div>";
// Handle category
$categoryId = insertIfNotExists($conn, 'categories', $category);
// Handle account
$accountId = insertIfNotExists($conn, 'accounts', $account);
// Handle vendor
$vendorId = insertIfNotExists($conn, 'vendors', $vendor);
// Insert product
$stmt = $conn->prepare("INSERT INTO products (name, description, buying_price, selling_price, quantity, category_id, account_id, vendor_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssddiiii", $name, $description, $buyingPrice, $sellingPrice, $quantity, $categoryId, $accountId, $vendorId);
if ($stmt->execute() === false) {
echo "<div class='alert alert-danger'>Failed to insert product: " . htmlspecialchars($stmt->error) . "</div>";
} else {
echo "<div class='alert alert-success'>Inserted product successfully.</div>";
}
}
// Function to insert data if not exists and return the ID
function insertIfNotExists($conn, $table, $name)
{
if ($name === null || trim($name) === '') {
return null; // Avoid inserting empty names
}
$stmt = $conn->prepare("SELECT id FROM $table WHERE name = ?");
$stmt->bind_param("s", $name);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
return $result->fetch_assoc()['id']; // Return existing ID
} else {
// Insert new record
$stmt = $conn->prepare("INSERT INTO $table (name) VALUES (?)");
$stmt->bind_param("s", $name);
if ($stmt->execute() === false) {
echo "<div class='alert alert-danger'>Failed to insert into $table: " . htmlspecialchars($stmt->error) . "</div>";
}
return $conn->insert_id; // Return new ID
}
}
// Check if a file is uploaded only when the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$filePath = $_FILES['file']['tmp_name'];
importData($filePath);
}
$conn->close();
?>
<?php require 'header.php'; require 'nav.php'; ?>
<div class="container mt-5">
<h2>Import Data</h2>
<form action="sql_import.php" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="file" class="form-label">Choose a file</label>
<input type="file" class="form-control" id="file" name="file" accept=".csv, .xlsx" required>
</div>
<button type="submit" class="btn btn-primary">Import Data</button>
</form>
</div>
<?php require 'footer.php'; ?>