-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit_message.php
90 lines (79 loc) · 3.54 KB
/
edit_message.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
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
<?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);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Update the message in the database
$message_id = $_POST['message_id'];
$message = $_POST['message'];
$sql = "UPDATE messages SET message = :message WHERE id = :message_id AND user_id = :user_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':message', $message, PDO::PARAM_STR);
$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();
} else {
// Retrieve the message from the database
$message_id = $_GET['id'];
$sql = "SELECT message 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();
$message = $stmt->fetchColumn();
}
} catch (PDOException $e) {
// Handle database connection error
$error = "Database error: " . $e->getMessage();
}
// Close the database connection
$conn = null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Message</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 font-sans leading-normal tracking-normal">
<header class="bg-white shadow-md">
<nav class="container mx-auto px-6 py-4">
<div class="flex items-center justify-between">
<div class="flex items-center">
<a href="index.html" class="text-xl font-bold text-gray-800">Kynlos Login Script</a>
</div>
<div class="flex items-center">
<a href="dashboard.php" class="mx-2 text-gray-600 hover:text-gray-800">Dashboard Logout
<main class="container mx-auto px-6 py-8">
<h2 class="text-3xl font-bold text-gray-800 mb-4">Edit Message</h2>
<?php if (isset($error)) { ?>
<p class="text-red-500 mb-4"><?php echo $error; ?></p>
<?php } ?>
<form action="edit_message.php" method="post" class="mb-8">
<input type="hidden" name="message_id" value="<?php echo $message_id; ?>">
<div class="mb-4">
<label for="message" class="block text-gray-700 font-bold mb-2">Message:</label>
<textarea id="message" name="message" rows="4" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" required><?php echo $message; ?></textarea>
</div>
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">Update Message</button>
</form>
</main>
<footer class="bg-gray-800 text-white py-4">
<div class="container mx-auto px-6 text-center">
© <?php echo date('Y'); ?> Kynlos Login Script. All rights reserved.
</div>
</footer>