-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_json.php
50 lines (43 loc) · 2.11 KB
/
upload_json.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
<?php
include 'db_config.php';
// Check if a file is uploaded
if (isset($_FILES['jsonFile']) && $_FILES['jsonFile']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['jsonFile']['tmp_name'];
$jsonData = file_get_contents($fileTmpPath);
// Decode the JSON data
$hotels = json_decode($jsonData, true);
if (isset($hotels['hotels'])) {
try {
// Check if the table is empty
$stmt = $pdo->query('SELECT COUNT(*) as count FROM hotels');
$row = $stmt->fetch();
if ($row['count'] > 0) {
echo '<script>alert("Data already exists in the database."); window.location.href = "myaccount.html";</script>';
} else {
// Prepare SQL query for inserting data
$insertStmt = $pdo->prepare(
'INSERT INTO hotels (hotel_id, hotel_name, city, available_rooms, date_available, price_per_night)
VALUES (:hotel_id, :hotel_name, :city, :available_rooms, :date_available, :price_per_night)'
);
foreach ($hotels['hotels'] as $hotel) {
$insertStmt->execute([
':hotel_id' => $hotel['hotel_id'],
':hotel_name' => $hotel['hotel_name'],
':city' => $hotel['city'],
':available_rooms' => $hotel['available_rooms'],
':date_available' => $hotel['date_available'],
':price_per_night' => $hotel['price_per_night'],
]);
}
echo '<script>alert("Data successfully added to the database."); window.location.href = "myaccount.html";</script>';
}
} catch (PDOException $e) {
echo '<script>alert("Error: ' . $e->getMessage() . '"); window.location.href = "myaccount.html";</script>';
}
} else {
echo '<script>alert("Invalid JSON file format."); window.location.href = "myaccount.html";</script>';
}
} else {
echo '<script>alert("Error uploading file."); window.location.href = "myaccount.html";</script>';
}
?>