-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_post.php
62 lines (50 loc) · 1.87 KB
/
update_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
<?php
session_start();
include 'dbconnection.php';
if (!isset($_SESSION['email'])) {
echo json_encode(['status' => 'error', 'message' => 'User not logged in']);
exit();
}
if (!isset($_POST['postId'])) {
echo json_encode(['status' => 'error', 'message' => 'Post ID not provided']);
exit();
}
$postId = $_POST['postId'];
$email = $_SESSION['email'];
// Fetch existing post data
$stmt = $pdo->prepare("SELECT * FROM post_table WHERE id = ?");
$stmt->execute([$postId]);
$post = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$post) {
echo json_encode(['status' => 'error', 'message' => 'Post not found']);
exit();
}
$caption = $_POST['editCaption'];
// Update caption
$stmt = $pdo->prepare("UPDATE post_table SET caption = ? WHERE id = ?");
$stmt->execute([$caption, $postId]);
// Handle image deletion
$imagesToDelete = isset($_POST['imagesToDelete']) ? json_decode($_POST['imagesToDelete'], true) : [];
$existingImages = json_decode($post['imagePost'], true);
$updatedImages = array_diff($existingImages, $imagesToDelete);
// Handle new images upload
$newImages = [];
if (!empty($_FILES['editImage']['name'][0])) {
$total = count($_FILES['editImage']['name']);
for ($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES['editImage']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = "Post_Images/" . basename($_FILES['editImage']['name'][$i]);
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
$newImages[] = basename($_FILES['editImage']['name'][$i]);
}
}
}
}
// Merge existing and new images
$allImages = array_merge($updatedImages, $newImages);
// Update images in the database
$stmt = $pdo->prepare("UPDATE post_table SET imagePost = ? WHERE id = ?");
$stmt->execute([json_encode($allImages), $postId]);
echo json_encode(['status' => 'success', 'message' => 'Post updated successfully']);
?>