-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_data.php
More file actions
47 lines (39 loc) · 1.2 KB
/
post_data.php
File metadata and controls
47 lines (39 loc) · 1.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
<?php
session_start();
include 'includes/db_connect.php';
// 1. Enable Error Reporting (For Debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// 2. Check Authentication
if (!isset($_SESSION['user_id'])) {
echo "Error: Not logged in";
exit();
}
// 3. Check Input Data
if (isset($_POST['booking_id']) && isset($_POST['message'])) {
// Force integer conversion to prevent SQL errors
$booking_id = intval($_POST['booking_id']);
$sender_id = intval($_SESSION['user_id']);
$message = trim($_POST['message']);
// 4. Validate Data
if ($booking_id <= 0) {
echo "Error: Invalid Booking ID";
exit();
}
if (!empty($message)) {
// Escape special characters to prevent SQL Injection
$message_safe = $conn->real_escape_string($message);
// 5. Insert Query
$sql = "INSERT INTO messages (booking_id, sender_id, message) VALUES ($booking_id, $sender_id, '$message_safe')";
if ($conn->query($sql) === TRUE) {
echo "Sent";
} else {
echo "Database Error: " . $conn->error;
}
} else {
echo "Error: Empty message";
}
} else {
echo "Error: Missing POST data";
}
?>