-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathupdate.php
More file actions
165 lines (148 loc) · 5.79 KB
/
update.php
File metadata and controls
165 lines (148 loc) · 5.79 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
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$name = $address = $salary = "";
$name_err = $address_err = $salary_err = "";
// Processing form data when form is submitted
if (isset($_POST["id"]) && !empty($_POST["id"])) {
// Get hidden input value
$id = $_POST["id"];
// Validate name
$input_name = trim($_POST["name"]);
if (empty($input_name)) {
$name_err = "Please enter a name.";
} elseif (!preg_match("/^[a-zA-Z\s]+$/", $input_name)) {
$name_err = "Please enter a valid name.";
} else {
$name = $input_name;
}
// Validate address
$input_address = trim($_POST["address"]);
if (empty($input_address)) {
$address_err = "Please enter an address.";
} else {
$address = $input_address;
}
// Validate salary
$input_salary = trim($_POST["salary"]);
if (empty($input_salary)) {
$salary_err = "Please enter the salary amount.";
} elseif (!ctype_digit($input_salary)) {
$salary_err = "Please enter a positive integer value.";
} else {
$salary = $input_salary;
}
// Check input errors before updating the database
if (empty($name_err) && empty($address_err) && empty($salary_err)) {
// Prepare an update statement
$sql = "UPDATE employees SET name = :name, address = :address, salary = :salary WHERE id = :id";
if ($stmt = $link->prepare($sql)) {
// Bind variables to the prepared statement as parameters
$stmt->bindValue(':name', $name, SQLITE3_TEXT);
$stmt->bindValue(':address', $address, SQLITE3_TEXT);
$stmt->bindValue(':salary', $salary, SQLITE3_INTEGER);
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
// Attempt to execute the prepared statement
if ($stmt->execute()) {
// Records updated successfully. Redirect to landing page
header("location: index.php");
exit();
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
}
// Close connection
$link->close();
} else {
// Check existence of id parameter before processing further
if (isset($_GET["id"]) && !empty(trim($_GET["id"]))) {
// Get URL parameter
$id = trim($_GET["id"]);
// Prepare a select statement
$sql = "SELECT * FROM employees WHERE id = :id";
if ($stmt = $link->prepare($sql)) {
// Bind variables to the prepared statement as parameters
$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
// Attempt to execute the prepared statement
$result = $stmt->execute();
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
// Retrieve individual field values
$name = $row["name"];
$address = $row["address"];
$salary = $row["salary"];
} else {
// URL doesn't contain a valid id. Redirect to error page
header("location: error.php");
exit();
}
} else {
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
$stmt->close();
// Close connection
$link->close();
} 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>Update Record</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
.wrapper{
width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="mt-5">Update Record</h2>
<p>Please edit the input values and submit to update the employee record.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control <?php echo (!empty($name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $name; ?>">
<span class="invalid-feedback"><?php echo $name_err;?></span>
</div>
<div class="form-group">
<label>Address</label>
<textarea name="address" class="form-control <?php echo (!empty($address_err)) ? 'is-invalid' : ''; ?>"><?php echo $address; ?></textarea>
<span class="invalid-feedback"><?php echo $address_err;?></span>
</div>
<div class="form-group">
<label>Salary</label>
<input type="text" name="salary" class="form-control <?php echo (!empty($salary_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $salary; ?>">
<span class="invalid-feedback"><?php echo $salary_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>