-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_product.php
More file actions
48 lines (43 loc) · 1.73 KB
/
update_product.php
File metadata and controls
48 lines (43 loc) · 1.73 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
<?php
include('config.php');
if (isset($_POST['update'])) {
$ID = $_POST['product_id'];
$NAME = $_POST['name'];
$PRICE = $_POST['price'];
$DESCRIPTION = $_POST['description'];
$STOCK = $_POST['stock'];
// Check if a new image was uploaded by verifying if the file name is not empty
if (isset($_FILES['image']) && !empty($_FILES['image']['name'])) {
$image_location = $_FILES['image']['tmp_name'];
$image_name = $_FILES['image']['name'];
$image_up = "assets/images/" . $image_name;
// Update including the image field
$update = "UPDATE products SET
name = '$NAME',
description = '$DESCRIPTION',
stock = '$STOCK',
price = '$PRICE',
image = '$image_up'
WHERE id = $ID";
mysqli_query($conn, $update);
// Attempt to move the uploaded file
if (move_uploaded_file($image_location, 'assets/images/' . $image_name)) {
echo "<script>alert('Product updated successfully with new image.');</script>";
} else {
echo "<script>alert('There was a problem uploading the image.');</script>";
}
} else {
// No new image provided; update only the other fields
$update = "UPDATE products SET
name = '$NAME',
description = '$DESCRIPTION',
stock = '$STOCK',
price = '$PRICE'
WHERE id = $ID";
mysqli_query($conn, $update);
echo "<script>alert('Product updated successfully without changing the image.');</script>";
}
header('location:products.php');
exit();
}
?>