-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_account.php
47 lines (37 loc) · 1.18 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
<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] != true) {
header("location: login.php");
exit;
}
// Check if the delete button is clicked
if (isset($_POST['delete'])) {
// Establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve logged in user's information (You should implement session management)
$username = $_SESSION['username'];
// SQL to delete user account
$sql = "DELETE FROM users WHERE username='$username'";
if ($conn->query($sql) === TRUE) {
// Unset all session variables
$_SESSION = array();
// Destroy the session
session_destroy();
echo "Account deleted successfully";
// Redirect to login page
header("Location: login.php");
exit();
} else {
echo "Error deleting account: " . $conn->error;
}
$conn->close();
}
?>