-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnt_add.php
More file actions
68 lines (56 loc) · 2 KB
/
Copy pathnt_add.php
File metadata and controls
68 lines (56 loc) · 2 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
<?php
/**
* Add Note Script
*
* This script handles the addition of a note to the database. It checks if the user is logged in, processes the form submission,
* and inserts the note into the 'nt_data' table. The response is returned as JSON.
*
*/
// Include the database connection file
include_once("dbcon.php");
// Start or resume the session if not already started
require 'session_config.php';
// Check if the user is logged in
if (!isset($_SESSION['username'])) {
echo json_encode(['success' => false, 'message' => 'You must be logged in to add a note.']);
exit;
}
// CSRF check
if ($_SERVER['REQUEST_METHOD'] !== 'POST'
|| !isset($_POST['csrf_token'])
|| !isset($_SESSION['csrf_token'])
|| !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'Invalid CSRF token.']);
exit;
}
// Handle form submission
$response = ['success' => false, 'message' => 'Invalid request.'];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['note_text'])) {
$note_text = $_POST['note_text'];
$user_id = $_SESSION['user_id'] ?? null;
$cage = isset($_POST['cage_id']) ? $_POST['cage_id'] : null;
// Prepare the SQL statement
$sql = "INSERT INTO notes (user_id, note_text, cage_id) VALUES (?, ?, ?)";
$stmt = $con->prepare($sql);
if ($stmt === false) {
$response['message'] = 'Prepare failed: ' . htmlspecialchars($con->error);
echo json_encode($response);
exit;
}
// Bind parameters
$stmt->bind_param("sss", $user_id, $note_text, $cage);
// Execute the statement
if ($stmt->execute()) {
$response['success'] = true;
$response['message'] = 'Note added successfully.';
} else {
$response['message'] = 'Execute failed: ' . htmlspecialchars($stmt->error);
}
// Close the statement
$stmt->close();
}
// Close the database connection
$con->close();
// Return the response as JSON
echo json_encode($response);