-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoin_group.php
More file actions
48 lines (36 loc) · 1.7 KB
/
Copy pathjoin_group.php
File metadata and controls
48 lines (36 loc) · 1.7 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
<?php
session_start(); // Start a new or resume the existing session
include "protected/connSql.php"; // Include the code to establish a database connection
// Print to the console for debugging purposes
error_log("I am in join_group");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if the user is logged in and has a valid session
if (isset($_SESSION["userId"])) {
// Validate and sanitize user inputs
$groupId = $_POST["groupId"];
// Define the user ID (fetched from the session)
$userId = $_SESSION["userId"];
// TODO: Replace this with the actual user role
$role = 1;
// Create an entry in the linkusergroup table
$linkInsertQuery = $conn->prepare("INSERT INTO linkUserGroup (userId, groupId, role) VALUES (?, ?, ?)");
$linkInsertQuery->bind_param("iii", $userId, $groupId, $role);
// Check if the entry creation is successful
if ($linkInsertQuery->execute()) {
// Linkusergroup entry creation successful, redirect to a success page or back to the group listing
header("Location: /classes/{$classId}");
} else {
// Error handling: Display an error message or redirect to an error page
echo "Failed to create a linkusergroup entry. Please try again.";
}
$linkInsertQuery->close(); // Close the prepared statement
} else {
// User is not logged in, handle the error
echo "Error: User ID is missing. Please log in and try again.";
}
} else {
// Handle an invalid request (not a POST request)
echo "Invalid request method. Please use the form to join a group.";
}
$conn->close(); // Close the database connection
?>