-
Notifications
You must be signed in to change notification settings - Fork 2
/
editbeach.php
150 lines (142 loc) · 6.5 KB
/
editbeach.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
<?php
require_once('../layouts/header.php');
session_start();
if ($_SESSION['role'] == "admin") {
} else {
header('location: index.php');
exit();
};
if (isset($_GET['id'])) {
$beachId = $_GET['id'];
$sql = "SELECT * FROM beach WHERE id='$beachId'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$beach = $result->fetch_assoc();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$description = $_POST['description'];
$category = $_POST['category'];
$price = $_POST['price'];
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$image_tmp = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
if (move_uploaded_file($image_tmp, $image_name)) {
$sql = "UPDATE beach SET name='$name', description='$description' , category='$category' , price='$price', image='../assets/$image_name' WHERE id='$beachId'";
$res = $conn->query($sql);
if ($res) {
header('Location: ../beachpage.php');
exit();
} else {
echo 'Error: ' . $conn->error;
}
} else {
echo 'Error: Failed to move the uploaded image.';
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="../assets/css/editbeach.css">
</head>
<body>
<div class="card">
<div class="card-header d-flex align-items-center justify-content-between">
<h3 class="text-center">Edit Beach Page</h3>
<?php
$username = $_SESSION['admin']['username'];
echo '<div class="d-flex align-items-center text-align-center">';
echo '<div>Hello Admin <h3>' . $username . '</h3></div>';
echo '<button onclick="dologout(event)" class="btn lout btn-primary">Logout</button>';
echo '</div>';
?>
</div>
<div class="page-wrap d-flex" style="margin-top:10px">
<div class="content-1">
<div class="control-wrap">
<h2 style="background-color: gray;">Admin Menu</h2>
<ul>
<li><a href="#">Admin</a></li>
<li><a href="#">Customer</a></li>
<li><a href="../beachpage.php">Beachs</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="feedback.php">Feedback</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</div>
<div class="content-2">
<div class="d-flex align-items-center justify-content-between mb-2">
<div>
<div class="title_editbeach" style="background-color: gray;">
<h2 style="margin-bottom: 50px;margin-left:43%;">Edit Beach</h2>
</div>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Beach Name</label>
<input type="text" name="name" id="name" value="<?php echo $beach['name']; ?>" required>
</div>
<div class="form-group">
<label for="description">Beach Description</label>
<textarea type="text" name="description" id="description"
value="<?php echo $beach['description']; ?>" required></textarea>
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="category" id="category" required>
<?php
$sql = "SELECT * from categories";
$res = $conn->query($sql);
if ($res->num_rows > 0) {
$rows = $res->fetch_all(MYSQLI_ASSOC);
foreach($rows as $cate){
echo '<option value="'. $cate['id'] . '">' .$cate['name'] .'</option>';
}
}
?>
</select>
</div>
<div class="form-group">
<label for="price">Beach Price</label>
<input type="text" name="price" id="price" value="<?php echo $beach['price']; ?>"
required>
</div>
<div class="form-group d-flex">
<label for="image">Beach Image</label>
<input type="file" name="image" id="image" accept="image/*" required>
<img id="preview-image" src="#" alt="Preview Image"
style="display: none; max-width: 200px; margin-top: 10px;">
</div>
<div class="form-group">
<label></label>
<button type="submit" class="btn btn-primary" name="update">Update Beach</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
function dologout(e) {
e.preventDefault();
if (confirm('Bạn có muốn đăng xuất không?')) {
window.location.href = '../logout.php';
}
}
document.getElementById('image').addEventListener('change', function (e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function (e) {
var imagePreview = document.getElementById('preview-image');
imagePreview.src = e.target.result;
imagePreview.style.display = 'block';
};
reader.readAsDataURL(file);
});
</script>
</html>