-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectUpdatePet.php
More file actions
414 lines (319 loc) · 15.6 KB
/
ProjectUpdatePet.php
File metadata and controls
414 lines (319 loc) · 15.6 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
///////////////////////////////////////////////////////
// Name: Nancy Mikyska
// Login: nancy
// Class: CIS-2360 Fall 2017
// Assignment: project UpdatePet
// File: ProjectUpdatePet.php
// Purpose: Pet assigned to adoptive person
///////////////////////////////////////////////////////
-->
<?php
///////////////////////////////////////
// STEP #2: Connect to the database. Do this only once, even if
// you have multiple queries on your page.
require_once("MySqlConnectStudentDb.php");
// get the pet Id
if($_SERVER["REQUEST_METHOD"] === "POST")
$petId = intval(filter_input(INPUT_POST, "pet_id"));
else
$petId = intval(filter_input(INPUT_GET, "pet"));
//if this is a post then update the db--
if($_SERVER["REQUEST_METHOD"] === "POST")
$updatedRows = updatePet();
else
$updatedRows = NULL;
$queryPets = "SELECT *
FROM Pets
WHERE PetId = ?;";
$stmtPets = mysqli_stmt_init($dbc);
if(!mysqli_stmt_prepare($stmtPets, $queryPets))
{
die("Failed to prepare statement.\n");
}
mysqli_stmt_bind_param($stmtPets, "i", $petId);
mysqli_stmt_execute($stmtPets);
$resultPets = mysqli_stmt_get_result($stmtPets);
////////////////////////////////////////////////////////////////
// Adoptions Query:
// STEP #1: Create PHP Variables for each variable that you need
// to bind to your SQL statements. Skip this step if your SQL
// SQL statement does not have bind variables (question marks).
// *** Skipped - no bind variables in SQL statement ***
// STEP #2: Connect to the database. Do this only once, even if
// you have multiple queries on your page.
//require_once("MySqlConnectStudentDb.php");
// STEP #3: Create your SQL statement as a string. Use bind
// variables (question marks) for any user input. Do not use
// user content directly in your SQL statement.
$queryAdopt = "SELECT *
FROM Adoptions
ORDER BY LastName,
FirstName;";
// STEP #4: Create and prepare the statement. The statement
// is the binary version of the statement
$stmtAdopt = mysqli_stmt_init($dbc);
if(!mysqli_stmt_prepare($stmtAdopt, $queryAdopt))
die("Failed to prepare statement\n");
// STEP #5: Bind the PHP variables from step #1 to the statement.
// Each question mark in the $query must be bound to the
// statement ($stmt). Only bind true variables (e.g. no function
// calls). Use "s" for strings, "d" for float (a.k.a. double),
// and "i" for integers. Skip this step if there are no bind variables
// in your SQL statement.
// mysqli_stmt_bind_param($stmt, "isd", $itemId, $itemDescription, $price);
// STEP #6: Execute statement and show any errors.
mysqli_stmt_execute($stmtAdopt);
if(mysqli_errno($dbc) !== 0)
{
echo("Error number: " . mysqli_errno($dbc));
echo("Error description: " . mysqli_error($dbc));
die();
}
// STEP #7: Get results. Use only for SELECT statements. Skip for
// INSERT, UPDATE, DELETE, etc.
$resultAdopt = mysqli_stmt_get_result($stmtAdopt);
// OPTIONAL STEP: Get the number of rows processed. Skip if you
// do not need to use the number of rows in your code.
// $numberRows = mysqli_affected_rows($dbc);
// OPTIONAL STEP: Debug statement examples. You can do this atan
// at any point for any variable.
// var_dump($numberRows);
// echo "<!-- \$numberRows " . $numberRows . "-->";
////////////////////////////////////////////////////////////////
// Vets Query:
// STEP #1: Create PHP Variables for each variable that you need
// to bind to your SQL statements. Skip this step if your SQL
// SQL statement does not have bind variables (question marks).
// *** Skipped - no bind variables in SQL statement ***
// STEP #2: Connect to the database. Do this only once, even if
// you have multiple queries on your page.
require_once("MySqlConnectStudentDb.php");
// STEP #3: Create your SQL statement as a string. Use bind
// variables (question marks) for any user input. Do not use
// user content directly in your SQL statement.
$queryVet = "SELECT *
FROM Vets
ORDER BY DrLastName,
DrFirstName;";
// STEP #4: Create and prepare the statement. The statement
// is the binary version of the statement
$stmtVet = mysqli_stmt_init($dbc);
if(!mysqli_stmt_prepare($stmtVet, $queryVet))
die("Failed to prepare statement\n");
// STEP #5: Bind the PHP variables from step #1 to the statement.
// Each question mark in the $query must be bound to the
// statement ($stmt). Only bind true variables (e.g. no function
// calls). Use "s" for strings, "d" for float (a.k.a. double),
// and "i" for integers. Skip this step if there are no bind variables
// in your SQL statement.
// mysqli_stmt_bind_param($stmt, "isd", $itemId, $itemDescription, $price);
// STEP #6: Execute statement and show any errors.
mysqli_stmt_execute($stmtVet);
if(mysqli_errno($dbc) !== 0)
{
echo("Error number: " . mysqli_errno($dbc));
echo("Error description: " . mysqli_error($dbc));
die();
}
// STEP #7: Get results. Use only for SELECT statements. Skip for
// INSERT, UPDATE, DELETE, etc.
$resultVet = mysqli_stmt_get_result($stmtVet);
// OPTIONAL STEP: Get the number of rows processed. Skip if you
// do not need to use the number of rows in your code.
// $numberRows = mysqli_affected_rows($dbc);
// OPTIONAL STEP: Debug statement examples. You can do this atan
// at any point for any variable.
// var_dump($numberRows);
// echo "<!-- \$numberRows " . $numberRows . "-->";
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Nancy M. - Project Update Pet</title>
<?php require("ProjectHeader.php"); ?> <!--call to header file-->
</head>
<body>
<div class="background">
<h1>Nancy M. - Project Update Pet</h1>
<?php
//update user if changes were made to the db
if($updatedRows === 0)
echo "<h2>No changes have been made.</h2>";
elseif($updatedRows === 1)
echo "<h2>Changes saved.</h2>";
?>
<br /><br />
<?php
//pull one record from the Sections query
if($petRow = mysqli_fetch_array($resultPets))
{
?>
<table>
<tr>
<td>Pet Name</td>
<td><?php echo $petRow["PetName"]; ?></td>
</tr>
<tr>
<td>MicroChip Id</td>
<td><?php echo $petRow["MicroChipId"]; ?></td>
</tr>
<tr>
<td>Breed</td>
<td><?php echo $petRow["Breed"]; ?></td>
</tr>
</table>
<br /><br />
<form action="ProjectUpdatePet.php" method="post">
<input type="hidden" name="pet_id" value="<?php echo $petRow["PetId"]; ?>" />
<table>
<tr>
<td>Adoptive Person</td>
<td>
<select name="adopt">
<option value="0">Select...</option>
<?php
//pull all records from the adoptions table in db
while($rowAdopt = mysqli_fetch_array($resultAdopt))
{
?>
<!--display currently set person for the pets, allow user to change the persons and save it to the db -->
<!--loop to display all persons in the dropdown field-->
<option value="<?php echo $rowAdopt['AdoptionId']; ?>" <?php if($rowAdopt['AdoptionId'] == $petRow['AdoptionId']) echo 'selected="selected"'; else echo ''; ?> >
<?php
echo htmlspecialchars($rowAdopt["LastName"]); //display persons last name
echo ", " . htmlspecialchars($rowAdopt["FirstName"]); //display persons first name
echo "</option>\n";
}?> <!--end persons loop-->
</select>
</td>
</tr>
<tr>
<td>Veterinarian</td>
<td>
<select name="vet">
<option value="0">Select...</option>
<?php
//pull all records from the vet table in db
while($rowVet = mysqli_fetch_array($resultVet))
{
?>
<!--display currently set vet for the pets, allow user to change the vet and save it to the db -->
<!--loop to display all vet in the dropdown field-->
<option value="<?php echo $rowVet['VetId']; ?>" <?php if($rowVet['VetId'] == $petRow['VetId']) echo 'selected="selected"'; else echo ''; ?> >
<?php
echo htmlspecialchars($rowVet["DrLastName"]); //display vet last name
echo ", " . htmlspecialchars($rowVet["DrFirstName"]); //display vet first name
echo "</option>\n";
}?> <!--end Vet loop-->
</select>
</td>
</tr>
<?php
} ?>
<tr>
<td>Spayed/Neutered Required</td>
<td>
<input type="hidden" name="spayed_neutered" value="0"<?php if($petRow["SpayedNeuteredNeededFlag"] == "0") echo ' checked=""'; ?> />
<input type="checkbox" name="spayed_neutered" value="1"<?php if ($petRow["SpayedNeuteredNeededFlag"] == "1") echo ' checked="checked"'; ?> />Spayed/Neutered
</td>
<!--display if the pet was spayed or not was set in the creation, allow user to change value and update to db-->
</tr>
<tr>
<td>Shots Required</td>
<td>
<input type="hidden" name="shots" value="0"<?php if($petRow["ShotsNeededFlag"] == "0") echo ' checked=""'; ?> />
<input type="checkbox" name="shots" value="1"<?php if ($petRow["ShotsNeededFlag"] == "1") echo ' checked="checked"'; ?> />Shots
</td>
<!--display if the pet was spayed or not was set in the creation, allow user to change value and update to db-->
</tr>
<?php
//close stmts and $dbc
mysqli_stmt_close($stmtAdopt);
mysqli_stmt_close($stmtVet);
mysqli_close($dbc);
?>
</table>
<br/><br/>
<input class="clickInput" type="submit" value="Update" />
<input class="clickInput" type="reset" />
</form>
<br /><br />
<!--links to other pages-->
<a href="ProjectDisplayAll.php" class="button">Back to Pet List</a>
<br /><br />
<br /><br />
<?php require("ProjectFooter.php"); ?> <!--call to footer file-->
</div>
</body>
</html>
<?php
//function to update the db
function updatePet()
{
global $dbc;
// STEP #1: Create PHP Variables for each variable that you need
// to bind to your SQL statements. Skip this step if your SQL
// SQL statement does not have bind variables (question marks).
$petId = filter_input(INPUT_POST, "pet_id");
$adopt = filter_input(INPUT_POST, "adopt");
$vet = filter_input(INPUT_POST, "vet");
$spayedNeutered = filter_input(INPUT_POST, "spayed_neutered");
$shots = filter_input(INPUT_POST, "shots");
// STEP #3: Create your SQL statement as a string. Use bind
// variables (question marks) for any user input. Do not use
// user content directly in your SQL statement.
$queryUpd = "UPDATE Pets
SET AdoptionId = ?,
VetId = ?,
SpayedNeuteredNeededFlag = ?,
ShotsNeededFlag = ?
WHERE PetId = ?;";
// STEP #4: Create and prepare the statement. The statement
// is the binary version of the statement
$stmtUpd = mysqli_stmt_init($dbc);
if(!mysqli_stmt_prepare($stmtUpd, $queryUpd))
{
echo "Failed to prepare statement on ";
echo "line " . __LINE__ . " in file " . __FILE__ . "<br />\n";
die();
}
// STEP #5: Bind the PHP variables from step #1 to the statement.
// Each question mark in the $query must be bound to the
// statement ($stmt). Only bind true variables (e.g. no function
// calls). Use "s" for strings, "d" for float (a.k.a. double),
// and "i" for integers. Skip this step if there are no bind variables
// in your SQL statement.
mysqli_stmt_bind_param($stmtUpd, "iissi", $adopt, $vet, $spayedNeutered, $shots, $petId);
// STEP #6: Execute statement and show any errors.
mysqli_stmt_execute($stmtUpd);
if(mysqli_errno($dbc) !== 0)
{
echo "Error number: " . mysqli_errno($dbc) . "<br />";
echo "Line " . __LINE__ . " in file " . __FILE__ . "<br />\n";
echo "Error description: " . mysqli_error($dbc) . "<br />";
die();
}
// STEP #7: Get results. Use only for SELECT statements. Skip for
// INSERT, UPDATE, DELETE, etc.
//$resultUpd = mysqli_stmt_get_result($stmtUpd);
// OPTIONAL STEP: Get the number of rows processed. Skip if you
// do not need to use the number of rows in your code.
$numberRows = mysqli_affected_rows($dbc);
// STEP #8: Read rows from the result set. Use a
// while loop if you expect more than one row. Use
// Use an if statement if expecting only one row.
// Include only for SELECT statements. Skip this
// step for INSERT, UPDATE, DELETE, etc.
/*while($row = mysqli_fetch_array($resultUpd))
{
echo "<li><a href=\"Demo16b.php?state=" . $row["StateCode"];
echo "\">" . $row["StateCode"] . "</a></li>\n";
}*/
// STEP #9: Close statement(s) and database connection.
mysqli_stmt_close($stmtUpd);
//mysqli_close($dbc);
return $numberRows;
}
?>