-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_user.php
67 lines (62 loc) · 2.46 KB
/
add_user.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
<?php
session_start();
$conn = new mysqli("localhost", "root", "", "learning_platform");
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
header("Location: login.php");
exit();
}
$error = ''; // To hold any error messages
$success = ''; // To hold success messages
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $conn->real_escape_string($_POST['name']);
$email = $conn->real_escape_string($_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Hash the password
$role = $conn->real_escape_string($_POST['role']);
// Check if the email already exists
$check_query = "SELECT * FROM users WHERE email = '$email'";
$check_result = $conn->query($check_query);
if ($check_result->num_rows > 0) {
$error = "A user with this email already exists.";
} else {
// Insert the user into the database
$query = "INSERT INTO users (name, email, password, role) VALUES ('$name', '$email', '$password', '$role')";
if ($conn->query($query)) {
$success = "User added successfully!";
} else {
$error = "Failed to add user. Please try again.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles_add_user.css">
<title>Add User</title>
</head>
<body>
<div class="container">
<h2>Add New User</h2>
<form method="POST" action="add_user.php">
<input type="text" name="name" placeholder="Full Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<select name="role" required>
<option value="student">Student</option>
<option value="teacher">Teacher</option>
<option value="admin">Admin</option>
</select>
<button type="submit">Add User</button>
</form>
<?php if (!empty($error)): ?>
<p style="color: red;"><?php echo $error; ?></p>
<?php endif; ?>
<?php if (!empty($success)): ?>
<p style="color: green;"><?php echo $success; ?></p>
<?php endif; ?>
<a href="dashboard_admin.php" class="back-btn">Back to Dashboard</a>
</div>
</body>
</html>