-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin.php
More file actions
120 lines (114 loc) · 5.72 KB
/
create_admin.php
File metadata and controls
120 lines (114 loc) · 5.72 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
session_start();
include 'config.php'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
include 'layout/header.php'
?>
</head>
<body>
<?php
include 'layout/nav.php'
?>
<main class="main">
<div class="container py-5">
<div class="row justify-content-center py-5">
<div class="login-container col-md-8 py-5">
<div class="card text-center py-4 px-3">
<h1>Create Admin</h1>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="register">
<div class="row mb-3">
<label for="email" class="col-md-4 col-form-label text-md-end">Email Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="" required autocomplete="email">
</div>
</div>
<div class="row mb-3">
<label for="password1" class="col-md-4 col-form-label text-md-end">Password</label>
<div class="col-md-6 position-relative">
<input id="password1" type="password" class="form-control" name="password1" required autocomplete="new-password">
<button type="button" id="togglePassword1" onclick="showPassword1()" class="btn shadow-none bg-transparent border-0 position-absolute top-0 end-0 me-1">
<i class="fa fa-eye-slash fs-4" aria-hidden="true"></i></button>
</div>
</div>
<div class="row mb-3">
<label for="password2" class="col-md-4 col-form-label text-md-end">Confirm Password</label>
<div class="col-md-6 position-relative">
<input id="password2" type="password" class="form-control" name="password2" required autocomplete="new-password">
<button type="button" id="togglePassword2" onclick="showPassword2()" class="btn shadow-none bg-transparent border-0 position-absolute top-0 end-0 me-1">
<i class="fa fa-eye-slash fs-4" aria-hidden="true"></i></button>
</div>
</div>
<div class="row mb-0">
<div class="col-md-11">
<button type="submit" class="login-btn">Register</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include 'layout/footer.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
$pwd = $_POST['password1'];
$confirmPwd = $_POST['password2'];
if ($pwd !== $confirmPwd) {
echo '<script>alert("Password are not match");</script>';
} else {
$sql = "SELECT * FROM users WHERE user_email = '$email' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
echo '<script>alert("User existed, please register a new user");</script>';
} else {
$pwdHash = trim(password_hash($pwd, PASSWORD_DEFAULT));
$sql = "INSERT INTO users (user_id, user_email, user_password, user_joinDate, user_role)
VALUES ('', '$email', '$pwdHash', CURRENT_TIMESTAMP, 'admin')";
if (mysqli_query($conn, $sql)) {
$lastInsertedId = mysqli_insert_id($conn);
$sql = "INSERT INTO admin (admin_id, user_id) VALUES ('','$lastInsertedId')";
if (mysqli_query($conn, $sql)) {
echo '<script>alert("Register Successfully. "); window.location.href = "admin_dashboard.php"</script>';
} else {
echo '<script>alert("Error");</script>';
}
} else {
echo '<script>alert("Error");</script>';
}
}
}
mysqli_close($conn);
}
?>
<script>
function showPassword1() {
var x = document.getElementById("password1");
let togglePassword = document.getElementById('togglePassword1')
if (x.type === "password") {
x.type = "text";
togglePassword.innerHTML = '<i class="fa fa-eye fs-4" aria-hidden="true">';
} else {
x.type = "password";
togglePassword.innerHTML = '<i class="fa fa-eye-slash fs-4" aria-hidden="true">';
}
}
function showPassword2() {
var x = document.getElementById("password2");
let togglePassword = document.getElementById('togglePassword2')
if (x.type === "password") {
x.type = "text";
togglePassword.innerHTML = '<i class="fa fa-eye fs-4" aria-hidden="true">';
} else {
x.type = "password";
togglePassword.innerHTML = '<i class="fa fa-eye-slash fs-4" aria-hidden="true">';
}
}
</script>
</main>
</body>
</html>