-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_insert.php
executable file
·134 lines (129 loc) · 4.34 KB
/
admin_insert.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
$title = "Admin Page";
include('header.php');
include('config/db.php');
$first_name = "";
$last_name = "";
$email = "";
$password = "";
$role = "";
$first_nameErr ="";
$last_nameErr ="";
$emailErr ="";
$passwordErr ="";
$sql = "SELECT * FROM tbl_other";
$result = $conn->query($sql);
if (!empty($result)) {
while ($row = $result->fetch_assoc()) {
$roles[] = $row;
}
}
if (isset($_POST['submit'])) {
$post = $_POST;
if(count($post) > 0) {
foreach ($post as $key => $value) {
$fields_map = array(
'first_name' => 'First Name',
'last_name' => 'Last Name',
'email' => 'Email',
'password' => 'Password',
'role' => '',
'submit' => ''
);
if($fields_map[$key] == 'First Name') {
$message = $fields_map[$key]." is required.";
$first_nameErr = $message;
}
if($fields_map[$key] == 'Last Name') {
$message = $fields_map[$key]." is required.";
$last_nameErr = $message;
}
if($fields_map[$key] == 'Email') {
$message = $fields_map[$key]." is required.";
$emailErr = $message;
}
if($fields_map[$key] == 'Password') {
$message = $fields_map[$key]." is required.";
$passwordErr = $message;
}
}
}
if((!empty($_POST['first_name'])) && (!empty($_POST['last_name'])) && (!empty($_POST['email'])) & (!empty($_POST['password']))) {
$first_name = $_POST['first_name'];
$first_nameErr = '';
$last_name = $_POST['last_name'];
$last_nameErr = '';
$emailErr = '';
$passwordErr = '';
$sql_tbl_info = "INSERT INTO tbl_info (first_name, last_name) VALUES ('".$first_name."', '".$last_name."')";
if ($conn->query($sql_tbl_info) === TRUE) {
$user_id = $conn->insert_id;
$email = $_POST['email'];
$password = $_POST['password'];
$role = $_POST['role'];
$sql_tbl_login = "INSERT INTO tbl_login (userid, role, email, password) VALUES ('".$user_id."', '".$role."', '".$email."', '".md5($password)."')";
if ($conn->query($sql_tbl_login) === TRUE) {
header("Location: index.php");
} else {
echo "Error: " . $sql_tbl_login . "<br>" . $conn->error;
}
} else {
echo "Error: " . $sql_tbl_info . "<br>" . $conn->error;
}
} else {
if((!empty($_POST['first_name']))) {
$first_name = $_POST['first_name'];
$first_nameErr = '';
}
if((!empty($_POST['last_name']))) {
$last_name = $_POST['last_name'];
$last_nameErr = '';
}
if((!empty($_POST['email']))) {
$email = $_POST['email'];
$emailErr = '';
}
if((!empty($_POST['password']))) {
$password = $_POST['password'];
$passwordErr = '';
}
}
}
$conn->close();
?>
<div class="col-md-4 mar-all">
<form method="post" action="Admin.php">
<div class="container">
<div class="form-group">
<label for="first_name">First Name</label>
<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Enter Frist Name" value="<?php echo htmlspecialchars($first_name);?>">
<span class="error"><?php echo $first_nameErr;?></span>
</div>
<div class="form-group">
<label for="last_name">Last Name</label>
<input type="text" class="form-control" name="last_name" id="last_name" placeholder="Enter Last Name" value="<?php echo htmlspecialchars($last_name);?>">
<span class="error"><?php echo $last_nameErr;?></span>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" id="email" placeholder="Enter Email" value="<?php echo htmlspecialchars($email);?>">
<span class="error"><?php echo $emailErr;?></span>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Enter Password" value="<?php echo htmlspecialchars($password);?>">
<span class="error"><?php echo $passwordErr;?></span>
</div>
<div class="form-group">
<label for="role">Role</label>
<select class="form-control" id="role" name="role">
<?php
foreach($roles as $key => $val) {
echo "<option value=".$val['id'].">".$val['role']."</option>";
} ?>
</select>
</div>
<button class="btn btn-lg btn-success" type="submit" name="submit">Submit</button>
</div>
</form>
</div>