-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_instruments.php
447 lines (370 loc) · 12.1 KB
/
edit_instruments.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/*
* edit_instruments.php
*
* A place to edit/update the instrument table
*
*/
include_once 'checkinstance.php';
if ( ( $_SESSION['userlevel'] != 3 ) &&
( $_SESSION['userlevel'] != 4 ) &&
( $_SESSION['userlevel'] != 5 ) ) // super, admin and superadmin only
{
header('Location: index.php');
exit();
}
include 'config.php';
include 'db.php';
// ini_set('display_errors', 'On');
// Are we being directed here from a push button?
if (isset($_POST['prior']))
{
do_prior($link);
exit();
}
else if (isset($_POST['next']))
{
do_next($link);
exit();
}
else if (isset($_POST['delete']))
{
do_delete($link);
exit();
}
else if (isset($_POST['update']))
{
do_update($link);
exit();
}
else if (isset($_POST['create']))
{
do_create($link);
exit();
}
// Start displaying page
$page_title = 'Edit Instruments';
$js = 'js/edit_instruments.js';
include 'header.php';
include 'lib/selectboxes.php';
?>
<!-- Begin page content -->
<div id='content'>
<h1 class="title">Edit Instruments</h1>
<!-- Place page content here -->
<?php if ( isset($_SESSION['message']) )
{
echo "<p class='message'>{$_SESSION['message']}</p>\n";
unset($_SESSION['message']);
} ?>
<?php
// Edit or display a record
if ( isset($_POST['edit']) )
edit_record($link);
else if ( isset($_POST['new']) )
do_new($link);
else
display_record($link);
?>
</div>
<?php
include 'footer.php';
exit();
// Function to redirect to prior record
function do_prior($link)
{
$instrumentID = $_POST['instrumentID'];
$query = "SELECT instrumentID FROM instrument " .
"ORDER BY name ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
// Find prior record
$current = null;
list($current) = mysqli_fetch_array($result);
while ($current != null && $instrumentID != $current)
{
$prior = $current;
list($current) = mysqli_fetch_array($result);
}
$redirect = ($prior == null) ? "" : "?ID=$prior";
header("Location: $_SERVER[PHP_SELF]$redirect");
exit();
}
// Function to redirect to next record
function do_next($link)
{
$instrumentID = $_POST['instrumentID'];
$query = "SELECT instrumentID FROM instrument " .
"ORDER BY name ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
// Find next record
$next = null;
while ($instrumentID != $next)
list($next) = mysqli_fetch_array($result);
list($next) = mysqli_fetch_array($result);
$redirect = ($next == null) ? "?ID=$instrumentID" : "?ID=$next";
header("Location: $_SERVER[PHP_SELF]$redirect");
exit();
}
// Function to delete the current record
function do_delete($link)
{
$query = "SELECT COUNT(*) FROM instrument";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
list( $count ) = mysqli_fetch_array( $result );
if ( $count < 2 )
{ // Skip deleting if no or only 1 instrument row exists
echo "<p>Cannot delete the last remaining instrument record.</p>\n";
//header("Location: $_SERVER[PHP_SELF]");
//exit();
return;
}
$instrumentID = $_POST['instrumentID'];
// Add checks here to prevent deleting records that can't be deleted
$query = "SELECT COUNT(*) FROM experiment " .
"WHERE instrumentID = $instrumentID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
list( $count ) = mysqli_fetch_array( $result );
if ( $count == 0 )
{
// Ok to delete
$query = "DELETE FROM permits " .
"WHERE instrumentID = $instrumentID ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$query = "DELETE FROM instrument " .
"WHERE instrumentID = $instrumentID ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
}
header("Location: $_SERVER[PHP_SELF]");
}
// Function to update the current record
function do_update($link)
{
$ID = $_SESSION['id'];
$instrumentID = $_POST['instrumentID'];
$labID = $_POST['labID'];
$name = addslashes(htmlentities($_POST['name']));
$serialNumber = addslashes(htmlentities($_POST['serialNumber']));
$query = "UPDATE instrument " .
"SET labID = $labID, " .
"name = '$name', " .
"serialNumber = '$serialNumber', " .
"dateUpdated = NOW() " .
"WHERE instrumentID = $instrumentID ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
header("Location: $_SERVER[PHP_SELF]?ID=$instrumentID");
exit();
}
// Function to create a new record
function do_create($link)
{
$labID = $_POST['labID'];
$name = addslashes(htmlentities($_POST['name']));
$serialNumber = addslashes(htmlentities($_POST['serialNumber']));
$query = "INSERT INTO instrument " .
"SET labID = $labID, " .
"name = '$name', " .
"serialNumber = '$serialNumber', " .
"dateUpdated = NOW() ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$new = mysqli_insert_id($link);
header("Location: {$_SERVER['PHP_SELF']}?ID=$new");
}
// Function to display and navigate records
function display_record($link)
{
// Find a record to display
$instrumentID = htmlentities(get_id($link));
if ($instrumentID === false)
return;
// Anything other than a number here is a security risk
if (!(is_numeric($instrumentID)))
return;
$query = "SELECT i.labID, i.name, lab.name, i.serialNumber " .
"FROM instrument i, lab " .
"WHERE instrumentID = ? " .
"AND i.labID = lab.labID ";
// Prepared statement
if ($stmt = mysqli_prepare($link, $query)) {
$stmt->bind_param('i', $instrumentID);
$stmt->execute();
$stmt->store_result();
$num_of_rows = $stmt->num_rows;
$stmt->bind_result( $i_labID, $i_name, $lab_name, $i_serial );
$stmt->fetch();
$stmt->free_result();
$stmt->close();
}
/* This code was replace by the prepared statement above
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Create local variables; make sure IE displays empty cells properly
foreach ($row as $key => $value)
{
$$key = (empty($value)) ? " " : html_entity_decode( stripslashes( nl2br($value) ) );
}
*/
// Populate a list box to allow user to jump to another record
$nav_listbox = "<select name='nav_box' id='nav_box' " .
" onchange='get_lab(this);' >" .
" <option value='null'>None selected...</option>\n";
$query = "SELECT instrumentID, name FROM instrument ORDER BY name";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
while (list($t_id, $t_name) = mysqli_fetch_array($result))
{
$selected = ($instrumentID == $t_id) ? " selected='selected'" : "";
$nav_listbox .= " <option$selected value='$t_id'>$t_name</option>\n";
}
$nav_listbox .= "</select>\n";
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method='post'>
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Instruments</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'>Jump to: $nav_listbox
<input type='submit' name='prior' value='<' />
<input type='submit' name='next' value='>' />
<input type='submit' name='new' value='New' />
<input type='submit' name='edit' value='Edit' />
<input type='submit' name='delete' value='Delete' />
<input type='hidden' name='instrumentID' value='$instrumentID' />
</td></tr>
</tfoot>
<tbody>
<tr><th>Lab:</th>
<td>$lab_name</td></tr>
<tr><th>Instrument Name:</th>
<td>$i_name</td></tr>
<tr><th>Serial Number:</th>
<td>$i_serial</td></tr>
</tbody>
</table>
</form>
HTML;
}
// Function to figure out which record to display
function get_id($link)
{
// See if we are being directed to a particular record
if (isset($_GET['ID']))
return( $_GET['ID'] );
// We don't know which record, so just find the first one
$query = "SELECT instrumentID FROM instrument " .
"ORDER BY name " .
"LIMIT 1 ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
if (mysqli_num_rows($result) == 1)
{
list($instrumentID) = mysqli_fetch_array($result);
return( $instrumentID );
}
// If we're here, there aren't any records
echo<<<HTML
<form action='{$_SERVER[PHP_SELF]}' method='post'>
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='2'>Edit Instruments</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='new' value='New' />
</td></tr>
</tfoot>
<tbody>
<tr><th>Status:</th>
<td>There are no records to display</td></tr>
</tbody>
</table>
</form>
HTML;
return( false );
}
// Function to edit a record
function edit_record($link)
{
// Get the record we need to edit
if ( isset( $_POST['edit'] ) )
$instrumentID = $_POST['instrumentID'];
else if ( isset( $_GET['edit'] ) )
$instrumentID = $_GET['edit'];
else
{
// How did we get here?
echo "<p>There was a problem with the edit request.</p>\n";
return;
}
$query = "SELECT labID, name, serialNumber " .
"FROM instrument " .
"WHERE instrumentID = $instrumentID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$row = mysqli_fetch_array($result);
$labID = $row['labID'];
$name = html_entity_decode( stripslashes( $row['name'] ) );
$serialNumber = html_entity_decode( stripslashes( $row['serialNumber'] ) );
$lab_text = lab_select( $link, "labID", $labID );
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Instruments</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='update' value='Update' />
<input type='hidden' name='instrumentID' value='$instrumentID' />
<input type='reset' /></td></tr>
</tfoot>
<tbody>
<tr><th>Lab:</th>
<td>$lab_text</td></tr>
<tr><th>Instrument Name:</th>
<td><textarea name='name' rows='6' cols='65'
wrap='virtual'>$name</textarea></td></tr>
<tr><th>Serial Number:</th>
<td><textarea name='serialNumber' rows='6' cols='65'
wrap='virtual'>$serialNumber</textarea></td></tr>
</tbody>
</table>
</form>
HTML;
}
// Function to create a new record
function do_new($link)
{
$lab_text = lab_select( $link, "labID" );
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Instruments</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='create' value='Create' />
<input type='reset' /></td></tr>
</tfoot>
<tbody>
<tr><th>Lab:</th>
<td>$lab_text</td></tr>
<tr><th>Instrument Name:</th>
<td><textarea name='name' rows='6' cols='65'
wrap='virtual'></textarea></td></tr>
<tr><th>Serial Number:</th>
<td><textarea name='serialNumber' rows='6' cols='65'
wrap='virtual'></textarea></td></tr>
</tbody>
</table>
</form>
HTML;
}
?>