-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateschmaint.php
292 lines (241 loc) · 11.8 KB
/
updateschmaint.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
// Include config file
if (isset($_GET["id"]) && !empty(trim($_GET["id"]))) {
// Include config file
require_once 'config.php';
} else {
echo "Something went wrong. Please try again later.";
//header("location: error.php");
exit();
}
// REPORT ALL MYSQL ERRORS
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Define variables and initialize with empty values
$mileageint = $monthint = $lastcompmiles = $nextschdmaint = 0;
$descr = "";
$mileageint_err = $monthint_err = $lastcompmiles_err = $lastcompdate_err = $nextschdmaint_err = $descr_err = "";
$input_mileageint = $input_monthint = $input_lastcompmiles = $input_nextschdmaint = 0;
$input_lastcompdate = $input_descr = "";
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
// Get hidden input value
// echo '<script>console.log("PHP Log:: $_POST Request")</script>';
$carid = $_POST["id"];
$svcrec_No = $_POST["svcrec_No"];
// Validate last completed date
$input_lastcompdate = $_POST["lastcompdate"];
// Validate Mileage Interval
$input_mileageint = trim($_POST["mileageint"]);
if (!empty($input_mileageint)) {
if (!ctype_digit($input_mileageint)) {
$mileageint_err = 'Please enter a positive integer value.';
} }
// Validate Month Interval
$input_monthint = trim($_POST["monthint"]);
if (!empty($input_monthint)) {
if (!ctype_digit($input_monthint)) {
$monthint_err = 'Please enter a positive integer value.';
} }
// Validate Last Completed Miles
$input_lastcompmiles = trim($_POST["lastcompmiles"]);
if (!empty($input_lastcompmiles)) {
if (!ctype_digit($input_lastcompmiles)) {
$lastcompmiles_err = 'Please enter a positive integer value.';
} }
// Validate Description
$input_descr = trim($_POST["descr"]);
if (empty($input_descr)) {
$descr_err = 'Please enter a description.';
}
// Check input errors before inserting in database
if (empty($mileage_err) && empty($monthint_err) && empty($lastcompmiles_err) && empty($descr_err)) {
// Prepare an update statement
// note the name=value pairs are not case sensitive for name but are case sensitive for value
$sql = "UPDATE tasks SET MileageInterval=:mileageint, MonthInterval=:monthint, LastCompletedMiles=:lastcompmiles, LastCompletedDate=:lastcompdate, NextSchedMaint=:nextschdmaint, Description=:descr
WHERE id=:carid and rec_No=:svcrec_No";
if ($stmt = $pdo->prepare($sql)) {
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':carid', $param_carid);
$stmt->bindParam(':svcrec_No', $param_svcrec_No);
$stmt->bindParam(':mileageint', $param_mileageint);
$stmt->bindParam(':monthint', $param_monthint);
$stmt->bindParam(':lastcompmiles', $param_lastcompmiles);
$stmt->bindParam(':lastcompdate', $param_lastcompdate);
$stmt->bindParam(':nextschdmaint', $param_nextschdmaint);
$stmt->bindParam(':descr', $param_descr);
// Set parameters
$param_carid = $carid;
$param_svcrec_No = $svcrec_No;
$param_mileageint = $input_mileageint;
$param_monthint = $input_monthint;
$param_lastcompmiles = $input_lastcompmiles;
$param_lastcompdate = $input_lastcompdate;
$param_nextschdmaint = $input_nextschdmaint;
$param_descr = $input_descr;
// Attempt to execute the prepared statement
if ($stmt->execute()) {
// Records created successfully. Redirect to landing page
header("location: carinfo.php");
exit();
} else {
echo "stmt-execute failed.";
PDOStatement::errorCode();
PDOStatement::errorInfo();
header("location: error.php");
exit();
}
} else {
echo "stmt-prepare failed";
PDOStatement::errorCode();
PDOStatement::errorInfo();
header("location: error.php");
exit();
}
// Close statement
unset($stmt);
}
// Close connection
unset($pdo);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Get URL parameter
$carid = trim($_GET["id"]);
$svcrec_No = trim($_GET["rec_No"]);
// Prepare a select statement
$sql = "SELECT * FROM tasks WHERE id = :carid and rec_No = :svcrec_No";
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':carid', $param_id);
$stmt->bindParam(':svcrec_No', $param_svcrec_No);
$param_id = $carid;
$param_svcrec_No = $svcrec_No;
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() == 1){
/* Fetch result row as an associative array. Since the result set
contains only one row, we don't need to use while loop */
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Retrieve individual field value
$input_mileageint = $row["MileageInterval"];
$input_monthint = $row["MonthInterval"];
$input_lastcompmiles = $row["LastCompletedMiles"];
$input_lastcompdate = $row["LastCompletedDate"];
$input_nextschdmaint = $row["NextSchedMaint"];
$input_descr = $row["Description"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "<p class='lead'><em>PDO error</em></p>";
}
}
// Close statement
unset($stmt);
// Close connection
unset($pdo);
} else{
// URL doesn't contain id parameter. Redirect to error page
echo "<p class='lead'><em>malformed URL.</em></p>";
header("location: error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html">
<title>CarQuest</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(function() {
$('#header').load('menu.html');
});
</script>
<style type="text/css">
<!--
body,td,th {
color: LightGray;
}
body {
background-color: SlateGray;
}
a:link {
color: White;
}
-->
input[type=text] {
border: 2px solid cyan;
border-radius: 4px;
background-color: SlateGray;
color: LightGray;
}
</style>
</head>
<body>
<div class="container">
<!–– displays menu ––>
<header>
<div id="header">
</div>
</header>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<!–– input form (name=) must match $_POST variable name ––>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($mileageint_err)) ? 'has-error' : ''; ?>">
<label>*Mileage Interval</label>
<input type="text" name="mileageint" size=8 value="<?php echo $input_mileageint; ?>">
<span class="help-block"><?php echo $mileageint_err;?></span>
</div>
<div class="form-group <?php echo (!empty($monthint_err)) ? 'has-error' : ''; ?>">
<label>*Month Interval</label>
<input type="text" name="monthint" size=8 value="<?php echo $input_monthint; ?>">
<span class="help-block"><?php echo $monthint_err;?></span>
</div>
<div class="form-group <?php echo (!empty($lastcompmiles_err)) ? 'has-error' : ''; ?>">
<label>*Last Completed (miles)</label>
<input type="text" name="lastcompmiles" size=8 value="<?php echo $input_lastcompmiles; ?>">
<span class="help-block"><?php echo $lastcompmiles_err;?></span>
</div>
<div class="form-group <?php echo (!empty($lastcompdate_err)) ? 'has-error' : ''; ?>">
<label>*Last Completed (Date)</label>
<input type="date" name="lastcompdate" required color: #000 value="<?php echo $input_lastcompdate; ?>">
<span class="help-block"><?php echo $lastcompdate_err;?></span>
</div>
<div class="form-group <?php echo (!empty($descr_err)) ? 'has-error' : ''; ?>">
<label>Description</label>
<input type="text" name="descr" size=45 required value="<?php echo $input_descr; ?>">
<span class="help-block"><?php echo $descr_err;?></span>
</div>
<div class="row">
<div class="col-md-12">
<input type="hidden" name="id" value="<?php echo $carid; ?>"/>
<input type="hidden" name="svcrec_No" value="<?php echo $svcrec_No; ?>"/>
<input type="submit" class="btn btn-success" value="Submit">
<a href="carinfo.php" class="btn btn-default">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>