-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_account.php
91 lines (63 loc) · 2.53 KB
/
delete_account.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
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: index.php');
exit;
}
require 'db/db_connect.php';
$conn = $con;
// Get user type and user ID
$user_id = $_SESSION['id']; // Assuming you store user_id in the session when the user logs in
$user_type = $_POST['user_type']; // 'coach', 'player', or 'user'
if ($user_type == 'coach') {
// If coach, delete all players linked to coach and their stats
$coach_id = $_SESSION['coach_id'];
$delete_coach = "DELETE FROM coaches WHERE coach_id = ?";
$delete_players = "DELETE FROM players WHERE coach_id = ?";
$delete_inv = "DELETE FROM invitations WHERE coach_id = ?";
// Prepare and execute both statements
$stmt = $conn->prepare($delete_coach);
$stmt->bind_param('i', $coach_id);
$stmt->execute();
$stmt = $conn->prepare($delete_players);
$stmt->bind_param('i', $coach_id);
$stmt->execute();
$delete_user = "DELETE FROM accounts WHERE id = ?";
// Prepare and execute the statement
$stmt = $conn->prepare($delete_user);
$stmt->bind_param('i', $user_id);
$stmt->execute();
echo "Coach account and all related data deleted successfully.";
} elseif ($user_type == 'player') {
$player_id = $_SESSION['player_id'];
// If player, delete player and their shooting data
$delete_player = "DELETE FROM players WHERE id = ?";
$delete_shots = "DELETE FROM shots WHERE player_id = ?";
// Prepare and execute both statements
$stmt = $conn->prepare($delete_player);
$stmt->bind_param('i', $player_id);
$stmt->execute();
$stmt = $conn->prepare($delete_shots);
$stmt->bind_param('i', $player_id);
$stmt->execute();
$delete_user = "DELETE FROM accounts WHERE id = ?";
// Prepare and execute the statement
$stmt = $conn->prepare($delete_user);
$stmt->bind_param('i', $user_id);
$stmt->execute();
echo "Player account and stats deleted successfully.";
} else {
// If regular user, delete just the user account
$delete_user = "DELETE FROM accounts WHERE id = ?";
// Prepare and execute the statement
$stmt = $conn->prepare($delete_user);
$stmt->bind_param('i', $user_id);
$stmt->execute();
echo "User account deleted successfully.";
}
// Logout the user and redirect to the homepage
session_destroy();
header("Location: success.php?b=index.php");
exit();