-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_post.php
64 lines (49 loc) · 1.89 KB
/
delete_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
<!-- delete_post.php -->
<?php
header('Content-Type: application/json');
session_start();
// Start output buffering to prevent premature output
ob_start();
// Include the database connection file
require 'db_connect.php'; // Adjust this path as necessary.
// Read the JSON input from the request body
$input = json_decode(file_get_contents("php://input"), true);
file_put_contents("debug.txt", print_r($input, true)); // This will write the input data to debug.txt file for inspection.
// Ensure the user is logged in
if (!isset($_SESSION['user_data'])) {
echo json_encode(['success' => false, 'error' => 'User not authenticated']);
exit;
}
// Check if the post ID was provided in the JSON input
if (!isset($input['post_id'])) {
echo json_encode(['success' => false, 'error' => 'Post ID not provided']);
exit;
}
$post_id = $input['post_id'];
$user_id = $_SESSION['user_data']['user_id'];
// Prepare SQL to delete the post only if it belongs to the user
$sql = "DELETE FROM posts WHERE post_id = ? AND user_id = ?";
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo json_encode(['success' => false, 'error' => 'Prepare failed: ' . $conn->error]);
exit;
}
// Bind the post ID and user ID to the prepared statement
$stmt->bind_param("ii", $post_id, $user_id);
// Execute the deletion
if (!$stmt->execute()) {
echo json_encode(['success' => false, 'error' => 'Execute failed: ' . $stmt->error]);
exit;
}
// Check if any rows were affected
if ($stmt->affected_rows > 0) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'No rows affected. Post ID may not exist or user may not have permission to delete this post.']);
}
// Close the statement and the database connection
$stmt->close();
$conn->close();
// Send output buffer and turn off output buffering
ob_end_flush();
?>