-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateUser.php
More file actions
executable file
·113 lines (100 loc) · 3.98 KB
/
createUser.php
File metadata and controls
executable file
·113 lines (100 loc) · 3.98 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
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$user_ID = $username = $password = "";
$user_ID_err = $username_err = $password_err = "";
//Processing form data
if($_SERVER["REQUEST_METHOD"] == "POST"){
//Validate username
$username = trim($_POST["username"]);
if(empty($username)){
$username_err = "Please enter a username.";
}
//Validate password
$password = trim($_POST["user_password"]);
if(empty($password)){
$password_err = "Please enter a valid password.";
}
//User ID is a random value between 100000 and 999999
$user_ID = rand(100000, 999999);
//Insert
if (empty($user_ID_err) && empty($username_err) && empty($password_err)) {
$sql = "INSERT INTO User (user_ID, username, password)
VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "iss", $param_user_ID, $param_username, $param_password);
// Set parameters
$param_user_ID = $user_ID;
$param_username = $username;
$param_password = $password;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "<center><h4>Error while creating new user</h4></center>";
$Ssn_err = "Error: user ID isn't unique. Try again";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title Review</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style type="text/css">
.wrapper{
width: 70%;
margin:0 auto;
}
table tr td:last-child a{
margin-right: 15px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
$('.selectpicker').selectpicker();
</script>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Create Record</h2>
</div>
<p>Please fill this form and submit to add a User record to the database.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err;?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="user_password" class="form-control" value="<?php echo $password; ?>">
<span class="help-block"><?php echo $password_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>