-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_blog.php
More file actions
37 lines (32 loc) · 1.06 KB
/
delete_blog.php
File metadata and controls
37 lines (32 loc) · 1.06 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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();
session_start();
require_once 'db.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
// Redirect to login page if not logged in
header('Location: login.php');
exit;
}
// Check if the blog ID is provided via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['blog_id'])) {
// Get the blog ID from the form data
$blogId = $_POST['blog_id'];
// Prepare a SQL statement to delete the blog post
$stmt = $db->prepare("DELETE FROM blog WHERE id = :id AND user_id = :user_id");
$stmt->bindParam(':id', $blogId);
$stmt->bindParam(':user_id', $_SESSION['user_id']);
// Execute the SQL statement
try {
$stmt->execute();
$_SESSION['success_message'] = "Blog post deleted successfully.";
} catch (PDOException $e) {
$_SESSION['error_message'] = "Error deleting blog post: " . $e->getMessage();
}
}
// Redirect back to the page where the user came from
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
?>