-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_post.php
166 lines (147 loc) · 5.79 KB
/
edit_post.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
<?php
require_once __DIR__ . '/allkoneksi/koneksi.php';
// Koneksi ke database
// Memeriksa apakah ada pengguna yang login
session_start();
$current_user = isset($_SESSION['username']) ? $_SESSION['username'] : '';
// Memeriksa apakah form telah disubmit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$Tags = $_POST['Tags'];
$uploaded_by = $current_user;
// Memeriksa apakah ada file gambar yang diupload
if ($_FILES['image']['name']) {
$image = $_FILES['image']['name'];
$target = "blogs/uploads/" . basename($image);
// Memindahkan file yang diupload ke folder tujuan
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
// Menghapus gambar lama
$sql = "SELECT image FROM posts WHERE id='$id'";
$result = $koneksi->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$old_image = $row['image'];
if (file_exists("blogs/uploads/" . $old_image)) {
unlink("blogs/uploads/" . $old_image);
}
}
// Update postingan dengan gambar baru
$sql = "UPDATE posts SET title='$title', content='$content', Tags='$Tags', image='$image' WHERE id='$id'";
}
} else {
// Update postingan tanpa mengubah gambar
$sql = "UPDATE posts SET title='$title', content='$content', Tags='$Tags' WHERE id='$id'";
}
if ($koneksi->query($sql) === TRUE) {
echo "<script>alert('Post berhasil diperbarui!');</script>";
echo "<script>location.href = 'index.php';</script>";
} else {
echo "Error: " . $sql . "<br>" . $koneksi->error;
}
}
// Mengambil data postingan untuk di-edit
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM posts WHERE id='$id' AND uploaded_by='$current_user'";
$result = $koneksi->query($sql);
if ($result->num_rows > 0) {
$post = $result->fetch_assoc();
} else {
echo "Post tidak ditemukan atau Anda tidak memiliki izin untuk mengedit post ini.";
exit;
}
} else {
exit;
}
$koneksi->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Post</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../assets/modules/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/modules/fontawesome/css/all.min.css">
<!-- CSS Libraries -->
<!-- Template CSS -->
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="stylesheet" href="../assets/css/components.css">
<!-- Start GA -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-94034622-3"></script>
</head>
<body>
<div class="container">
<h1>Edit Post</h1>
<form action="edit_post.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($post['id']); ?>">
<div class="mb-3">
<label for="title" class="form-label">Judul</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($post['title']); ?>" required>
</div>
<div class="mb-3">
<label for="content" class="form-label">Deskripsi</label>
<textarea class="form-control" id="content" name="content" rows="4" required><?php echo htmlspecialchars($post['content']); ?></textarea>
</div>
<div class="mb-3">
<label for="Tags" class="form-label">Tags</label>
<input type="text" class="form-control" id="Tags" name="Tags" value="<?php echo htmlspecialchars($post['Tags']); ?>" required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Gambar</label>
<input type="file" class="form-control" id="image" name="image">
<img src="blogs/uploads/<?php echo htmlspecialchars($post['image']); ?>" class="img-fluid mt-2" alt="Current Image">
</div>
<button type="submit" class="btn btn-primary yell">Update Post</button>
<a href="index.php" class="btn btn-danger ori">Kembali</a>
</form>
</div>
<style>
.yell {
position: relative;
top: -7px;
}
.ori {
position: relative;
top: -7px;
}
.yell:hover {
border-radius: 40px;
background-color: blue;
transition: 0.5s;
}
.ori:hover {
background-color: aliceblue;
border-radius: 40px;
transition: 0.6s;
}
</style>
<script>
function backer() {
Swal.fire({
title: "Apakah Anda yakin?",
text: "Anda tidak akan dapat mengembalikannya!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Ya, keluar!"
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
text: "Anda telah keluar.",
icon: "success"
});
setTimeout(function() {
window.location.href = "index.php";
}, 2000);
}
});
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>