-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_user.php
More file actions
46 lines (38 loc) · 1.35 KB
/
update_user.php
File metadata and controls
46 lines (38 loc) · 1.35 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
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Ensure the user is an admin
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header("Location: login.php");
exit();
}
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "zoo_website";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get user data from POST request
$user_id = $_POST['user_id'];
$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$phone_number = $_POST['phone_number'];
$role = $_POST['role'];
// Prepare and bind
$sql = "UPDATE users SET name = ?, username = ?, email = ?, phone_number = ?, role = ? WHERE user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssssi", $name, $username, $email, $phone_number, $role, $user_id);
// Execute the statement
if ($stmt->execute()) {
echo "<script>alert('User updated successfully'); window.location.href = 'manage_users.php';</script>";
} else {
echo "<script>alert('Error updating user: " . $stmt->error . "'); window.history.back();</script>";
}
$stmt->close();
$conn->close();
}
?>