-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
219 lines (196 loc) · 8.62 KB
/
update.php
File metadata and controls
219 lines (196 loc) · 8.62 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
<?php
// Include config file
require_once "conntodb.php";
// Define variables and initialize with empty values
$brand = $model = $price = $stock = $status = $description = "";
$brand_err = $model_err = $price_err = $stock_err = $status_err = "";
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
// Get hidden input value
$id = $_POST["id"];
$input_description = trim($_POST["description"]);
$description = $input_description;
$input_brand = trim($_POST["brand"]);
if(empty($input_brand)){
$brand_err = "Please enter the brand.";
} else{
$brand = $input_brand;
}
$input_model = trim($_POST["model"]);
if(empty($input_model)){
$model_err = "Please enter the model.";
} else{
$model = $input_model;
}
$input_price = trim($_POST["price"]);
if(empty($input_price)){
$price_err = "Please enter the price amount.";
} elseif(!ctype_digit($input_price)){
$price_err = "Please enter a positive integer value.";
} else{
$price = $input_price;
}
$input_stock = trim($_POST["stock"]);
if(empty($input_stock)){
$stock_err = "Please enter the stock amount.";
} elseif(!ctype_digit($input_stock)){
$stock_err = "Please enter a integer value.";
} else{
$stock = $input_stock;
}
$input_status = trim($_POST["status"]);
if(empty($input_status)){
$status_err = "Please enter the status.";
} elseif(!ctype_digit($input_status)){
$status_err = "Please enter a positive integer value.";
} else{
$status = $input_status;
}
// Check input errors before inserting in database
if(empty($price_err) && empty($stock_err) && empty($status_err) && empty($brand_err) && empty($model_err)){
// Prepare an update statement
$sql = "UPDATE car SET brand=?, model=?, price=?, stock=?, status=?, description=? WHERE id=?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssssi", $param_brand, $param_model, $param_price, $param_stock, $param_status, $param_description, $param_id);
// Set parameters
$param_brand = $brand;
$param_model = $model;
$param_price = $price;
$param_stock = $stock;
$param_status = $status;
$param_description = $description;
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: admin.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
} 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 car WHERE id = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 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 = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$brand = $row["brand"];
$model = $row["model"];
$price = $row["price"];
$stock = $row["stock"];
$status = $row["status"];
$description = $row["description"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} 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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style>
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<body>
<?php
session_start();
if (!isset($_SESSION['memberid']) || !$_SESSION['isAdmin']) {
header('HTTP/1.0 403 Forbidden');
echo "<h1>Forbidden</h1>";
echo "You must have admin privileges to access this page.";
}
else {
?>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<p>Please edit the input values and submit to update the record.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($brand_err)) ? 'has-error' : ''; ?>">
<label>Brand</label>
<input type="text" name="brand" class="form-control" value="<?php echo $brand; ?>">
<span class="help-block"><?php echo $brand_err;?></span>
</div>
<div class="form-group <?php echo (!empty($model_err)) ? 'has-error' : ''; ?>">
<label>Model</label>
<input type="text" name="model" class="form-control" value="<?php echo $model; ?>">
<span class="help-block"><?php echo $model_err;?></span>
</div>
<div class="form-group <?php echo (!empty($price_err)) ? 'has-error' : ''; ?>">
<label>Price</label>
<input type="number" name="price" class="form-control" value="<?php echo $price; ?>">
<span class="help-block"><?php echo $price_err;?></span>
</div>
<div class="form-group <?php echo (!empty($stock_err)) ? 'has-error' : ''; ?>">
<label>Stock</label>
<input type="number" name="stock" class="form-control" value="<?php echo $stock; ?>">
<span class="help-block"><?php echo $stock_err;?></span>
</div>
<div class="form-group <?php echo (!empty($status_err)) ? 'has-error' : ''; ?>">
<label>Status</label>
<input type="number" min="1" max="4" name="status" class="form-control" value="<?php echo $status; ?>">
<span class="help-block"><?php echo $status_err;?></span>
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control"><?php echo $description; ?></textarea>
<span class="help-block"></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="admin.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<?php }