-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete_message.php
More file actions
36 lines (30 loc) · 976 Bytes
/
delete_message.php
File metadata and controls
36 lines (30 loc) · 976 Bytes
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
<?php
// Start session
session_start();
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// Database connection
$db_file = realpath(dirname(__FILE__) . '/users.db');
try {
$conn = new PDO("sqlite:$db_file");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Delete the message from the database
$message_id = $_GET['id'];
$sql = "DELETE FROM messages WHERE id = :message_id AND user_id = :user_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':message_id', $message_id, PDO::PARAM_INT);
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
// Redirect back to user messages page
header("Location: user_messages.php");
exit();
} catch (PDOException $e) {
// Handle database connection error
$error = "Database error: " . $e->getMessage();
}
// Close the database connection
$conn = null;
?>