-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_post.php
More file actions
174 lines (146 loc) · 3.98 KB
/
edit_post.php
File metadata and controls
174 lines (146 loc) · 3.98 KB
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
169
170
171
172
173
174
<?php
include 'db.php';
// Start the session
session_start();
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Get the logged-in user's ID
$user_id = $_SESSION['user_id'];
// Get the post ID from the URL
$post_id = $_GET['post_id'] ?? 0;
$post_id = intval($post_id); // Sanitize input
// Fetch the post details
$sql = "SELECT * FROM posts WHERE id = $post_id AND user_id = $user_id";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
// Post does not exist or user doesn't have permission to edit it
echo "Post not found or you don't have permission to edit this post.";
exit();
}
$post = $result->fetch_assoc();
// Handle form submission to update the post
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST['title'];
$content = $_POST['content'];
$category = $_POST['category'];
$pet_category = $_POST['pet_category'];
// Update the post in the database
$stmt = $conn->prepare("UPDATE posts SET title = ?, content = ?, category = ?, pet_category = ? WHERE id = ? AND user_id = ?");
$stmt->bind_param("ssssii", $title, $content, $category, $pet_category, $post_id, $user_id);
if ($stmt->execute()) {
// Redirect to the post details page after update
header("Location: post_detail.php?post_id=" . $post_id);
exit();
} else {
echo "Error updating post.";
}
$stmt->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 - Petiverse</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f9f9f9;
color: #333;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
h1 {
font-size: 2.5rem;
color: #4CAF50;
text-align: center;
margin-bottom: 1rem;
}
/* Form container */
form {
background: linear-gradient(135deg, #6a9bfa, #a0d7f5);
padding: 2rem;
border-radius: 10px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
max-width: 500px;
width: 100%;
}
/* Labels */
label {
font-weight: bold;
color: #fff;
display: block;
margin-bottom: 0.5rem;
}
/* Inputs and textarea */
input[type="text"],
textarea {
width: 100%;
padding: 0.8rem;
margin-bottom: 1.5rem;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
font-size: 1rem;
background-color: #fff;
}
textarea {
resize: vertical;
min-height: 100px;
}
/* Submit button */
button[type="submit"] {
display: block;
width: 100%;
padding: 0.8rem;
background: #4CAF50;
border: none;
border-radius: 5px;
color: #fff;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease;
}
button[type="submit"]:hover {
background: #3a8d41;
}
/* Responsive design */
@media (max-width: 600px) {
form {
padding: 1.5rem;
}
h1 {
font-size: 2rem;
}
}
</style>
</head>
<body>
<h1>Edit Post</h1>
<form action="" method="POST">
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="<?php echo htmlspecialchars($post['title']); ?>" required>
<br>
<label for="content">Content:</label>
<textarea name="content" id="content" required><?php echo htmlspecialchars($post['content']); ?></textarea>
<br>
<label for="category">Category:</label>
<input type="text" name="category" id="category" value="<?php echo htmlspecialchars($post['category']); ?>" required>
<br>
<label for="pet_category">Pet Category:</label>
<input type="text" name="pet_category" id="pet_category" value="<?php echo htmlspecialchars($post['pet_category']); ?>" required>
<br>
<button type="submit">Update Post</button>
</form>
</body>
</html>
<?php $conn->close(); ?>