-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_note.php
More file actions
168 lines (144 loc) · 4.75 KB
/
Copy pathupdate_note.php
File metadata and controls
168 lines (144 loc) · 4.75 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php"); // Redirect to login page if not logged in
exit;
}
include 'db.php';
// Check if note ID is provided
if (isset($_GET['note_id'])) {
$note_id = $_GET['note_id'];
// Fetch the note from the database
$sql = "SELECT * FROM notes WHERE note_id = $note_id AND user_id = " . $_SESSION['user_id'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$note = $result->fetch_assoc();
} else {
echo "Note not found or you do not have permission to update it.";
exit;
}
}
// Handle form submission for updating the note
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['note_text'])) {
$note_text = $_POST['note_text'];
$image_path = $note['image_path']; // Keep the old image path if no new image is uploaded
// Check if a new image is uploaded
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
$image_name = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
$target_dir = 'uploads/';
$target_file = $target_dir . basename($image_name);
// Ensure the target directory exists
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true); // Create directory if it doesn't exist
}
// Check file size (Optional: max 2MB)
if ($_FILES['image']['size'] > 2000000) {
echo "File is too large. Maximum size is 2MB.";
} else {
// Attempt to move the uploaded file to the target directory
if (move_uploaded_file($image_tmp, $target_file)) {
$image_path = $target_file;
} else {
echo "Error uploading the file.";
}
}
}
// Update the note in the database
$sql = "UPDATE notes SET note_text = '$note_text', image_path = '$image_path' WHERE note_id = $note_id";
if ($conn->query($sql) === TRUE) {
header("Location: landing.php"); // Redirect to landing page after update
exit;
} else {
echo "Error: " . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Note</title>
<style>
/* Add your CSS here */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f1f1f1;
color: #333;
padding: 30px;
}
.update-container {
width: 80%;
margin: 0 auto;
max-width: 600px;
padding: 20px;
background-color: white;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #4CAF50;
}
form {
display: flex;
flex-direction: column;
}
textarea, input[type="text"], input[type="file"] {
padding: 12px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 12px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.note-image {
max-width: 100%;
margin-top: 10px;
border-radius: 5px;
}
.back-link {
display: block;
text-align: center;
margin-top: 20px;
font-size: 1.2em;
color: #4CAF50;
}
.back-link a {
color: #4CAF50;
text-decoration: none;
}
.back-link a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="update-container">
<h1>Update Note</h1>
<form action="update_note.php?note_id=<?php echo $note['note_id']; ?>" method="POST" enctype="multipart/form-data">
<textarea name="note_text" placeholder="Update your note..." required><?php echo $note['note_text']; ?></textarea><br>
<?php if ($note['image_path']): ?>
<img src="<?php echo $note['image_path']; ?>" class="note-image" alt="Note Image">
<p>Current Image: <?php echo basename($note['image_path']); ?></p>
<?php endif; ?>
<input type="file" name="image"><br>
<button type="submit">Update Note</button>
</form>
<div class="back-link">
<a href="landing.php">Back to Landing Page</a>
</div>
</div>
</body>
</html>