-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditAsset.php
165 lines (142 loc) · 6.11 KB
/
EditAsset.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
<?php
// Login Credentials required.
require('../includes/ProtectMB.php');
// Destroy session at time limit.
require('../includes/SessionExpire.php');
//Turn on error reporting
ini_set('display_errors', 'On');
// Import dBase Credentials.
require('../../project/g3f2Kcd57nE4s25.php');
// Connect to the database.
$mysqli = new mysqli($servername, $username, $password, $database);
?>
<!DOCTYPE HTML>
<html>
<head>
<?php
include('../includes/HeadMB.php');
require('../includes/Sanitizer.php');
?>
<script type="text/javascript">
// Clears prepopulated form following Update Asset submit button click.
function clearForm() {
document.getElementById("id").value="";
document.getElementById("Name").value="";
document.getElementById("Description").value="";
document.getElementById("Radius").value="";
document.getElementById("Mass").value="";
document.getElementById("ApMag").value="";
document.getElementById("cDate").value="";
document.getElementById("Owned_By").value="";
}
</script>
</head>
<body>
<h1>MoonBuyers InterGalactic</h1>
<h2>Edit An Asset</h2>
<div class="container">
<h3>Orginal Asset Details</h3>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th class="text-center bold">Asset ID</th>
<th class="text-center bold">Name</th>
<th class="text-center bold">Description</th>
<th class="text-center bold">Radius</th>
<th class="text-center bold">Mass</th>
<th class="text-center bold">Apparent Magnitude</th>
<th class="text-center bold">Create<span class="span-hide">_</span>Date</th>
<th class="text-center bold">Owned By</th>
</tr>
</thead>
<tbody>
<?php // Get Customer records.
if (isset($_POST['EditAsset'])) {
if (!($stmt = $mysqli->prepare("SELECT id, Name, Description, Radius, Mass, ApMag, Create_Date, Owned_By
FROM Asset WHERE id=?"))) {
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if (!($stmt->bind_param("i", $_POST['EditAsset']))) {
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if (!$stmt->bind_result($id, $Name, $Description, $Radius, $Mass, $ApMag, $Create_Date, $Owned_By)) {
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while ($stmt->fetch()) {
echo "<tr id=\"table-borders-override\">\n<td>\n" . $id . "\n</td>\n<td>\n" . $Name . "\n</td>\n<td>\n" . $Description . "\n</td>\n<td>\n" . $Radius . "\n</td>\n<td>\n" . $Mass . "\n</td>\n<td>\n" . $ApMag . "\n</td>\n<td>\n" . $Create_Date . "\n</td>\n<td>\n" . $Owned_By . "\n</td>\n</tr>";
}
$stmt->close();
}
?>
</tbody>
</table>
</div>
<br><br>
<div>
<h3>Data Entry</h3>
<form method="post">
<div class="form-group container">
<?php //assetID_ css display: none - this field needed as reference to $_POST['EditAsset'] passed from IndexMB.php is lost on submit. ?>
<input class="form-control" type="text" name="assetID_" id="assetID_" value="<?php echo $id ?>" readonly>
<input class="form-control" type="text" name="Name" id="Name" placeholder="Name (Corrected)" value="<?php echo $Name ?>" required>
<input class="form-control" type="text" name="Descr" id="Descr" placeholder="Description of New Asset (Corrected)" value="<?php echo $Description ?>" required>
<input class="form-control" type="number" name="Radius" id="Radius" min="0" step="0.01" placeholder="Radius in Kilometers (Corrected)" value="<?php echo $Radius ?>" required>
<input class="form-control" type="number" name="Mass" id="Mass" min="0" step="0.01" placeholder="Mass in Kilograms (Corrected)" value="<?php echo $Mass ?>" required>
<input class="form-control" type="number" name="ApMag" id="ApMag" min="-30" max="30" step="0.001" placeholder="Apparent Magnitude (Corrected)" value="<?php echo $ApMag ?>" required>
<br><br>
<input type="submit" value="Submit Asset Changes" name="submit" id="submit">
</div>
</form>
<br>
<button type="button" class="button" onclick="location.href = 'IndexMB.php';">Return to Main Page</button>
<br><br>
</div>
<?php
/* Form handler - Executes on 'Update Asset' submit button clicked. */
if (isset($_POST['submit'])) {
/* Prepare statement for UPDATE Asset's details. */
if (!($stmt = $mysqli->prepare("UPDATE Asset SET Name=?, Description=?, Radius=?, Mass=?, ApMag=?
WHERE id=?"))) {
echo "<p class=\"error\">Prepare for Asset UPDATE query failed: " . $stmt->errno . " " . $stmt->error . "</p>" ;
}
// Sanitize user input.
$cleaner = new Cleaner();
$_name = $cleaner->CleanString($_POST['Name']);
$_descr = $cleaner->CleanString($_POST['Descr']);
$_radius = $cleaner->CleanDecimal($_POST['Radius']);
$_mass = $cleaner->CleanDecimal($_POST['Mass']);
$_apMag = $cleaner->CleanDecimal($_POST['ApMag']);
/* Bind Parameters for UPDATE Asset's details. */
if (!($stmt->bind_param("ssdddi", $_name, $_descr, $_radius, $_mass, $_apMag, $_POST['assetID_']))) {
echo "<p class=\"error\">Bind failed: " . $stmt->errno . " " . $stmt->error . "</p>";
}
/* Execute UPDATE Asset's details. */
if (!$stmt->execute()) {
echo "<p class=\"error\">Execute failed: " . $stmt->errno . " " . $stmt->error . "</p>";
}
else {
echo "<p class=\"success\">Updated " . $stmt->affected_rows . " rows in table \"Asset\".</p>";
echo "<p class=\"success\">Form has been cleared.</p>";
}
$stmt->close();
// Clear the form!
echo "<script type=\"text/javascript\">clearForm();</script>";
}
?>
<!-- Decimal Places Control. -->
<script
src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous">
</script>
<script>
$("#Radius, #Mass").blur(function() {
this.value = Math.abs(parseFloat(this.value).toFixed(2));
});
$("#ApMag").blur(function() {
this.value = parseFloat(this.value).toFixed(3);
});
</script>
</body>
</html>