-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
169 lines (144 loc) · 6.38 KB
/
update.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
<?php
include_once "style.php";
?>
<?php
//include database connection
include 'database.php';
// get passed parameter value, in this case, the record ID
// isset() is a PHP function used to verify if a value is there or not
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
// read current record's data
try {
// prepare select query
$query = "SELECT id, name, description, price FROM products WHERE id = ? LIMIT 0,1";
$stmt = $con->prepare($query);
// this is the first question mark
$stmt->bindParam(1, $id);
// execute our query
$stmt->execute();
// store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// values to fill up our form
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
} // show error
catch (PDOException $exception) {
die('ERROR: ' . $exception->getMessage());
}
?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
// check if form was submitted
if ($_POST) {
try {
$query = "UPDATE products
SET name=:name, description=:description, price=:price
WHERE id = :id";
// prepare query for excecution
$stmt = $con->prepare($query);
// posted values
$name = htmlspecialchars(strip_tags($_POST['name']));
$description = htmlspecialchars(strip_tags($_POST['description']));
$price = htmlspecialchars(strip_tags($_POST['price']));
// bind the parameters
$stmt->bindParam(':name', $name);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':price', $price);
$stmt->bindParam(':id', $id);
// Execute the query
if ($stmt->execute()) {
echo "<div class='alert alert-success alert-dismissible fade in'>
<p>Record was updated</p>
<a href='index.php' class=''>
<span class='glyphicon glyphicon-chevron-left' aria-hidden='true'></span> Back to list
</a>
</div>";
} else {
echo 'Unable to update record. Please try again.';
}
} // show errors
catch (PDOException $exception) {
die('ERROR: ' . $exception->getMessage());
}
}
?>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<h3 id="glyphicons" class="page-header">Update Product
<small>Simple CRUD by KhoaHA</small>
</h3>
<div class="x_content">
<form action='update.php?id=<?php echo htmlspecialchars($id); ?>' method='post'
class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Product Name <span
class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name" required="required"
class="form-control col-md-7 col-xs-12"
data-parsley-id="4235"
value="<?php echo htmlspecialchars($name, ENT_QUOTES); ?>">
<ul class="parsley-errors-list" id="parsley-id-4235"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="description">Description <span
class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="description" name="description" required="required"
class="form-control col-md-7 col-xs-12"
data-parsley-id="4235" value="<?php echo htmlspecialchars($description, ENT_NOQUOTES); ?>">
<ul class="parsley-errors-list" id="parsley-id-4235"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="price">Price <span
class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="price" name="price" required="required"
class="form-control col-md-7 col-xs-12"
data-parsley-id="4235"
value="<?php echo htmlspecialchars($price, ENT_QUOTES); ?>"/>
<ul class="parsley-errors-list" id="parsley-id-4235"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12"></span>
</label>
<button type="submit" class="btn btn-primary col-md-6 col-sm-6 col-xs-12"
data-target=".bs-example-modal-sm">
<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span> Update
</button>
</div>
</form>
</div>
</div>
</div>
<!--<!--we have our html form here where new user information will be entered-->
<!--<form action='update.php?id=--><?php //echo htmlspecialchars($id); ?><!--' method='post' border='0'>-->
<!-- <table>-->
<!-- <tr>-->
<!-- <td>Name</td>-->
<!-- <td><input type='text' name='name' value="--><?php //echo htmlspecialchars($name, ENT_QUOTES); ?><!--"/></td>-->
<!-- </tr>-->
<!-- <tr>-->
<!-- <td>Description</td>-->
<!-- <td><textarea name='description'>--><?php //echo htmlspecialchars($description, ENT_QUOTES); ?><!--</textarea></td>-->
<!-- </tr>-->
<!-- <tr>-->
<!-- <td>Price</td>-->
<!-- <td><input type='text' name='price' value="--><?php //echo htmlspecialchars($price, ENT_QUOTES); ?><!--"/></td>-->
<!-- </tr>-->
<!-- <tr>-->
<!-- <td></td>-->
<!-- <td>-->
<!-- <input type='submit' value='Save Changes'/>-->
<!-- <a href='index.php'>Back to read records</a>-->
<!-- </td>-->
<!-- </tr>-->
<!-- </table>-->
<!--</form>-->