-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-product.php
184 lines (169 loc) · 8.17 KB
/
edit-product.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Изменить товар - TinyGeek</title>
<link rel="stylesheet" href="assets/styles/AdminPanelStyles.css">
<link rel="icon" href="assets/images/logo_icon.webp">
</head>
<?php
include_once('assets/scripts/db_connect.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=cursed', 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
die("Ошибка подключения: " . $e->getMessage());
}
session_start();
if (isset($_SESSION['login']))
{
$login = $_SESSION['login'];
$sql = "SELECT * FROM Users WHERE login = '".$_SESSION['login']."'";
$result = mysqli_query($link, $sql);
$roledata = mysqli_fetch_all($result);
$role = $roledata[0][5];
}
if (isset($role)) {
if ($role !== 'Admin') {
header('Location: access_denied.php');
exit();
}
} else {
header('Location: entry.php');
exit();
}
?>
<body>
<div class="alert">
<span class="closealert" onclick="this.parentElement.style.display='none';">×</span>
<img class="imgalert" src="./assets/images/warning.webp">
<strong>Внимание!</strong> Данный сайт разработан как курсовой проект, и магазин не является действующим!
</div>
<div class="topnav">
<a class="inactive" href="#">+7 (343) 307-37-84</a>
<div class="topnav-right">
<img class="locationimg" src="./assets/images/location.webp">
<a class="inactive" href="#">Екатеринбург</a>
</div>
</div>
<div class="bottomnav">
<img class="navlogo" src="./assets/images/logo_detailed.webp">
<a class="inactive" href="#">TinyGeek</a>
<a href="./main.php">Главная</a>
<a href="./products.php">Все продукты</a>
<a href="./categories.php">Категории</a>
<a href="./contacts.php">Контакты</a>
<div class="topnav-right">
<?
if(isset($role)) {
if ($role == 'Admin') {
echo '<a class="active" href="./admin-panel.php">Администрирование</a>';
echo '<a href="./log-out.php">Выход</a>';
} elseif ($role == 'Client') {
echo '<a href="./cart.php">Корзина</a>';
echo '<a href="./log-out.php">Выход</a>';
}
}
else {
echo '<a href="./entry.php">Войти</a>';
}
?>
</div>
</div>
<div class="product-add-block">
<h1 class="admin-header">Редактирование товара</h3>
</div>
<div class="add-prod-container">
<div class="add-prod">
<?php
if (isset($_GET['id'])) {
$idProd = $_GET['id'];
$sql = "SELECT * FROM Products WHERE idProduct = :idProduct";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':idProduct', $idProd, PDO::PARAM_INT);
$stmt->execute();
$array_prod = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($array_prod as $var) {
echo '
<form method="POST" enctype="multipart/form-data">
<input type="text" name="add-prod-name" value="'.$var['ProductName'].'" class="add-prod-name" required placeholder="Название товара">
<input type="text" name="add-prod-price" value="'.$var['ProductPrice'].'" class="add-prod-price" required placeholder="Цена товара">
<label class="input-file">
<input type="file" name="add-prod-image" class="add-prod-image">
<span class="choose-img">Выберите изображение</span>
</label>
<label class="label-description">Описание товара</label>
<textarea name="add-prod-desc" class="add-prod-desc" required>'.$var['ProductDesc'].'</textarea>
<label>Категория</label>
<select name="select-category" class="select-category">';
$sql = "SELECT * FROM Categories";
$stmt = $conn->prepare($sql);
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($array as $item) {
$selected = ($item['idCategory'] == $var['idCategory']) ? 'selected="selected"' : '';
echo '<option value="'.$item['idCategory'].'" '.$selected.'>'.$item['CategoryName'].'</option>';
}
echo
'</select>
<input type="submit" name="edit-product-button" value="Редактировать" class="add-product-button">
</form>';
}
if (isset($_POST['edit-product-button'])) {
$idCategory = $_POST['select-category'];
$ProductName = strval($_POST['add-prod-name']);
$ProductPrice = intval($_POST['add-prod-price']);
$ProductDesc = strval($_POST["add-prod-desc"]);
if (!empty($_FILES['add-prod-image']['tmp_name'])) {
$ProductImage = $_FILES['add-prod-image']['tmp_name'];
$filedata = file_get_contents($ProductImage);
$stmt = $conn->prepare("UPDATE Products SET idCategory = :IDcategory, ProductName = :ProdName, ProductImage = :ProdImage, ProductPrice = :ProdPrice, ProductDesc = :ProdDesc WHERE idProduct = :idProduct");
$stmt->bindValue(':IDcategory', $idCategory, PDO::PARAM_INT);
$stmt->bindValue(':ProdName', $ProductName, PDO::PARAM_STR);
$stmt->bindValue(':ProdImage', $filedata, PDO::PARAM_LOB);
$stmt->bindValue(':ProdPrice', $ProductPrice, PDO::PARAM_INT);
$stmt->bindValue(':ProdDesc', $ProductDesc, PDO::PARAM_STR);
$stmt->bindValue(':idProduct', $idProd, PDO::PARAM_INT);
if ($stmt->execute()) {
header("Refresh:0");
} else {
echo 'Ошибка: ' . implode(", ", $stmt->errorInfo());
}
} else {
$stmt = $conn->prepare("UPDATE Products SET idCategory = :IDcategory, ProductName = :ProdName, ProductPrice = :ProdPrice, ProductDesc = :ProdDesc WHERE idProduct = :idProduct");
$stmt->bindValue(':IDcategory', $idCategory, PDO::PARAM_INT);
$stmt->bindValue(':ProdName', $ProductName, PDO::PARAM_STR);
$stmt->bindValue(':ProdPrice', $ProductPrice, PDO::PARAM_INT);
$stmt->bindValue(':ProdDesc', $ProductDesc, PDO::PARAM_STR);
$stmt->bindValue(':idProduct', $idProd, PDO::PARAM_INT);
if ($stmt->execute()) {
header("Refresh:0");
} else {
echo 'Ошибка: ' . implode(", ", $stmt->errorInfo());
}
}
}
}
?>
</div>
</div>
<footer>
<ul class="social">
<li><a href="https://github.com/osakered"><img src="./assets/images/github-logo.webp" alt="Гитхаб"></a></li>
<li><a href="https://vk.com/"><img src="./assets/images/gamno-logo.webp" alt="ВК"></a></li>
<li><a href="https://desktop.telegram.org/"><img src="./assets/images/telegram-logo.webp" alt="Телеграм"></a></li>
</ul>
<ul class="menu">
<li></li>
<li><a href="./main.php">Главная</a></li>
<li><a href="./products.php">Продукты</a></li>
<li><a href="./categories.php">Категории</a></li>
<li><a href="./contacts.php">Контакты</a></li>
</ul>
<p>©2024-2025 Site by Osakered | No Rights Reserved</p>
</footer>
</body>
</html>