-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateEmployee.php
More file actions
executable file
·225 lines (200 loc) · 8.18 KB
/
Copy pathupdateEmployee.php
File metadata and controls
executable file
·225 lines (200 loc) · 8.18 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
session_start();
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
// Note: You can not update SSN
$Lname = $Fname = $Salary = $Bdate = $Address = $Sex = $Dno = $Super_ssn = "";
$Lname_err = $Fname_err = $Address_err = $Sex_err = $Salary_err = $Dno_err = "" ;
// Form default values
if(isset($_GET["Ssn"]) && !empty(trim($_GET["Ssn"]))){
$_SESSION["Ssn"] = $_GET["Ssn"];
// Prepare a select statement
$sql1 = "SELECT * FROM EMPLOYEE WHERE Ssn = ?";
if($stmt1 = mysqli_prepare($link, $sql1)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt1, "s", $param_Ssn);
// Set parameters
$param_Ssn = trim($_GET["Ssn"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt1)){
$result1 = mysqli_stmt_get_result($stmt1);
if(mysqli_num_rows($result1) > 0){
$row = mysqli_fetch_array($result1);
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$Salery = $row['Salary'];
$Bdate = $row['Bdate'];
$Address = $row['Address'];
$Sex = $row['Sex'];
$Dno = $row['Dno'];
$Super_ssn = $row['Super_ssn'];
}
}
}
}
// Post information about the employee when the form is submitted
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// the id is hidden and can not be changed
$Ssn = $_SESSION["Ssn"];
// Validate form data this is similar to the create Employee file
// Validate name
$Fname = trim($_POST["Fname"]);
if(empty($Fname)){
$Fname_err = "Please enter a first name.";
} elseif(!filter_var($Fname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$Fname_err = "Please enter a valid first name.";
}
$Lname = trim($_POST["Lname"]);
if(empty($Lname)){
$Lname_err = "Please enter a last name.";
} elseif(!filter_var($Lname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$Lname_err = "Please enter a valid last name.";
}
// Validate Address
$Address = trim($_POST["Address"]);
if(empty($Address)){
$Address_err = "Please enter Address.";
}
// Validate Salary
$Salary = trim($_POST["Salary"]);
if(empty($Salary)){
$Salary_err = "Please enter salary.";
}
// Validate Department Number
$Dno = trim($_POST["Dno"]);
if(empty($Dno)){
$Dno_err = "Please enter department number.";
}
// Check input errors before inserting into database
if(empty($Fname_err) && empty($Lname_err) && empty($Address_err) && empty($Salary_err) && empty($Dno_err)){
// Prepare an update statement
$sql = "UPDATE EMPLOYEE SET Fname=?, Lname=?, Address=?, Salary = ?, Dno = ? WHERE Ssn=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssdis", $param_Fname, $param_Lname,$param_Address, $param_Salary,$param_Dno, $param_Ssn);
// Set parameters
$param_Fname = $Fname;
$param_Lname = $Lname;
$param_Address = $Address;
$param_Salary = $Salary;
$param_Dno = $Dno;
$param_Ssn = $Ssn;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "<center><h2>Error when updating</center></h2>";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} else {
// Check existence of sID parameter before processing further
// Form default values
if(isset($_GET["Ssn"]) && !empty(trim($_GET["Ssn"]))){
$_SESSION["Ssn"] = $_GET["Ssn"];
// Prepare a select statement
$sql1 = "SELECT * FROM EMPLOYEE WHERE Ssn = ?";
if($stmt1 = mysqli_prepare($link, $sql1)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt1, "s", $param_Ssn);
// Set parameters
$param_Ssn = trim($_GET["Ssn"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt1)){
$result1 = mysqli_stmt_get_result($stmt1);
if(mysqli_num_rows($result1) == 1){
$row = mysqli_fetch_array($result1);
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$Salary = $row['Salary'];
$Bdate = $row['Bdate'];
$Address = $row['Address'];
$Sex = $row['Sex'];
$Dno = $row['Dno'];
$Super_ssn = $row['Super_ssn'];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Error in SSN while updating";
}
}
// Close statement
mysqli_stmt_close($stmt1);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Company DB</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h3>Update Record for SSN = <?php echo $_GET["Ssn"]; ?> </H3>
</div>
<p>Please edit the input values and submit to update.
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($Fname_err)) ? 'has-error' : ''; ?>">
<label>First Name</label>
<input type="text" name="Fname" class="form-control" value="<?php echo $Fname; ?>">
<span class="help-block"><?php echo $Fname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Lname_err)) ? 'has-error' : ''; ?>">
<label>Last Name</label>
<input type="text" name="Lname" class="form-control" value="<?php echo $Lname; ?>">
<span class="help-block"><?php echo $Lname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Address_err)) ? 'has-error' : ''; ?>">
<label>Address</label>
<input type="text" name="Address" class="form-control" value="<?php echo $Address; ?>">
<span class="help-block"><?php echo $Address_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Salary_err)) ? 'has-error' : ''; ?>">
<label>Salary</label>
<input type="text" name="Salary" class="form-control" value="<?php echo $Salary; ?>">
<span class="help-block"><?php echo $Salary_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Dno_err)) ? 'has-error' : ''; ?>">
<label>Department Number</label>
<input type="number" min="1" max="20" name="Dno" class="form-control" value="<?php echo $Dno; ?>">
<span class="help-block"><?php echo $Dno_err;?></span>
</div>
<input type="hidden" name="Ssn" value="<?php echo $Ssn; ?>"/>
<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>