-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
149 lines (141 loc) · 5.72 KB
/
update.php
File metadata and controls
149 lines (141 loc) · 5.72 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
<?php
include './component/header_as_admin.php';
$error = '';
if( !isset( $_GET['edit'])) {
header('Location: ./upload_san_pham.php');
}
$edit_id = $_GET['edit'];
?>
<div style="margin-left: 300px;" >
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table>
<?php
$sql_update = "SELECT * FROM Products WHERE productId = $edit_id";
try {
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $connection->prepare($sql_update);
$statement->execute();
$statement->setFetchMode ( PDO::FETCH_ASSOC);
$product = $statement->fetchAll();
foreach ( $product as $product) {
?>
<tr>
<td>Picture of product </td>
<td>
<?php
$id = $product['productId'];
$sql = "SELECT * FROM Image_Product WHERE productId = '$id'; ";
$statement = $connection->prepare($sql);
$statement->execute();
//Chế độ đọc dữ liệu ra
$result = $statement->setFetchMode ( PDO::FETCH_ASSOC);
$img = $statement->fetchAll();
$anh_sp = '';
foreach( $img as $img) {
$anh_sp = explode(', ', $img['imageUrls']);
}
foreach( $anh_sp as $anh_sp) {
?>
<img style="width: 100px;" src="./Ảnh_sp/<?php echo $anh_sp;?>">
<?php
}
?>
</td>
</tr>
<tr>
<input type="hidden" name="id" value="<?php echo $product['productId'];?>" id="">
</tr>
<tr>
<td>Name: </td>
<td><input type="text" class="box" name="update_name" value="<?php echo $product['productName'];?>" id=""></td>
</tr>
<tr>
<td>Weight: </td>
<td><input type="number" min="0" class="box" name="update_weight" value="<?php echo $product['weight'];?>"></td>
</tr>
<tr>
<td>Color: </td>
<td><input type="text" class="box" name="update_color" value="<?php echo $product['color'];?>" id=""></td>
</tr>
<tr>
<td>Category: </td>
<td>
<input type="text" class="box" name="update_category" value="<?php echo $product['categoryName'];?>" id="">
</td>
</tr>
<tr>
<td>Gender: </td>
<td>
<input type="text" class="box" name="update_gender" value="<?php echo $product['gender'];?>" id="">
</tr>
<tr>
<td>Brand: </td>
<td>
<input type="text" class="box" name="update_brand" value="<?php echo $product['brand'];?>" id="">
</td>
</tr>
<tr>
<td>Picture:</td>
<td>
<input type="file" class="box" name="update_image[]" accept="image/png, image/jpg, image/jpeg" multiple>
</td>
</tr>
<tr>
<td>Price: </td>
<td><input type="number" name="price" min=0 id="" require value="<?php echo $product['price'];?>">$</td>
</tr>
<?php
}
} catch(PDOException $e) {
echo "Cannot execute sql: " . $e->getMessage();
}
?>
</table>
<p style="color: red;"><?php echo $error;?></p>
<input type="submit" value="Update" name="update_product" class="btn-lg" id="">
<input type="submit" value="Cancel" name="cancel" class="btn-lg">
</form>
</div>
<?php
if ( isset($_POST['update_product'])) {
$id = $_POST['id'];
$update_name = $_POST['update_name'] ?? '';
$update_price = $_POST['price'] ?? '';
$update_weight = $_POST['update_weight'] ?? '';
$update_color = $_POST['update_color'] ?? '';
$update_brand = $_POST['update_brand'] ?? '';
$update_gender = $_POST['update_gender'] ?? '';
$update_category = $_POST['update_category'] ?? '';
$update_image = $_FILES['update_image']['name'] ?? '';
$count_img = 0;
$destination[] = "";
for ( $i =0; $i<5; $i++) {
if ( $update_image[$i] ) {
$count_img++;
}
if ( empty($update_image[$i + 1]) ) {
break;
}
$destination = "./Ảnh_sp/".$update_name[$i];
}
$update_image_tmp_name = $_FILES['update_image']['tmp_name'];
$anh_sp = "";
for ( $i =0; $i < $count_img; $i++) {
move_uploaded_file( $update_image_tmp_name[$i], $destination[$i]);
$anh_sp .= $update_image[$i].', ';
}
$anh_sp = rtrim( $anh_sp, ", ");
echo $anh_sp;
$sql = "UPDATE Products
SET productName = '$update_name', weight = '$update_weight',
color = '$update_color', price = '$update_price', categoryName = '$update_category',
brand = '$update_brand', gender = '$update_gender' WHERE productId = '$id';
UPDATE Image_Product SET imageUrls = '$anh_sp' WHERE productId = '$id';";
$connection->prepare( $sql )->execute();
header('Location: ./upload_san_pham.php');
}
else if ( isset($_POST['cancel'])) {
header('Location: ./upload_san_pham.php');
}
include './component/footer.php';
?>