-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.php
146 lines (116 loc) · 4.88 KB
/
notes.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
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
<?php
session_start();
include 'db_config.php';
if (!isset($_SESSION['username'])) {
header("Location: login.php");
exit;
}
$username = $_SESSION['username'];
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['create_note'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$notes_table_name = $username . "_notes";
$title = mysqli_real_escape_string($conn, $title);
$content = mysqli_real_escape_string($conn, $content);
$sql = "INSERT INTO `$notes_table_name` (`title`, `content`) VALUES ('$title', '$content')";
$result = $conn->query($sql);
if ($result) {
$message = "Note created successfully";
} else {
$message = "Error creating note: " . $conn->error;
}
}
if (isset($_GET['delete'])) {
$note_id = $_GET['delete'];
$notes_table_name = $username . "_notes";
$sql = "DELETE FROM `$notes_table_name` WHERE `id` = $note_id";
$result = $conn->query($sql);
if ($result) {
$message = "Note deleted successfully";
} else {
$message = "Error deleting note: " . $conn->error;
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['edit_note'])) {
$note_id = $_POST['note_id'];
$title = $_POST['title'];
$content = $_POST['content'];
$notes_table_name = $username . "_notes";
$title = mysqli_real_escape_string($conn, $title);
$content = mysqli_real_escape_string($conn, $content);
$sql = "UPDATE `$notes_table_name` SET `title`='$title', `content`='$content' WHERE `id`='$note_id'";
$result = $conn->query($sql);
if ($result) {
$message = "Note updated successfully";
} else {
$message = "Error updating note: " . $conn->error;
}
}
$notes_table_name = $username . "_notes";
$sql = "SELECT * FROM `$notes_table_name`";
$result = $conn->query($sql);
$notes = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$notes[] = $row;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notes</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2>Welcome, <?php echo $username; ?>!</h2>
<h3>Create a New Note</h3>
<?php if (isset($message)) echo "<p>$message</p>"; ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="title" placeholder="Title" style="width: 300px;" required><br>
<textarea name="content" placeholder="Content" rows="4" style="width: 300px;" required></textarea><br>
<button type="submit" name="create_note" style="width: 100px; margin-top: 10px;">Create Note</button>
</form>
<h3>Your Notes</h3>
<?php if (!empty($notes)) : ?>
<ul>
<?php foreach ($notes as $note) : ?>
<li>
<strong><?php echo $note['title']; ?></strong>
<p><?php echo $note['content']; ?></p>
<button class="edit-btn" style="width: 100px; margin-top: 10px;">Edit</button>
<form class="edit-form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" style="display: none;">
<input type="hidden" name="note_id" value="<?php echo $note['id']; ?>">
<input type="text" name="title" value="<?php echo $note['title']; ?>" style="width: 300px;" required><br>
<textarea name="content" rows="2" style="width: 300px;" required><?php echo $note['content']; ?></textarea><br>
<button type="submit" name="edit_note" style="width: 100px; margin-top: 10px;">Save</button>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get" style="display: inline;">
<input type="hidden" name="delete" value="<?php echo $note['id']; ?>">
<button type="submit" style="width: 100px; margin-top: 10px;">Delete</button>
</form>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p>No notes found.</p>
<?php endif; ?>
<p><a href="logout.php">Logout</a></p>
</div>
<script>
document.querySelectorAll('.edit-btn').forEach(function(button) {
button.addEventListener('click', function(event) {
var editForm = this.nextElementSibling;
if (editForm.style.display === 'none') {
editForm.style.display = 'block';
} else {
editForm.style.display = 'none';
}
});
});
</script>
</body>
</html>