-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddProductProcess.php
More file actions
122 lines (103 loc) · 3.95 KB
/
addProductProcess.php
File metadata and controls
122 lines (103 loc) · 3.95 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
<?php
session_start();
include "db/connections.php";
$email = $_SESSION["sup"]["username"];
$category = $_POST["ca"];
$brand = $_POST["b"];
$model = $_POST["m"];
$title = $_POST["t"];
$cost = $_POST["co"];
$desc = $_POST["de"];
$qty = $_POST["qty"];
// Input Validation
if (empty($title)) {
echo "Please Enter Your product title.";
exit;
} else if (empty($desc)) {
echo "Please Enter Your product details.";
exit;
} else if (empty($qty)) {
echo "Please enter your product qty.";
exit;
} else if ($qty <= 1) {
echo "Product qty must be greater than 1.";
exit;
} else if (empty($category)) {
echo "Please select your product category.";
exit;
} else if (empty($model)) {
echo "Please select your product model.";
exit;
} else if (empty($brand)) {
echo "Please select your brand.";
exit;
} else if (empty($cost)) {
echo "Please enter your product price.";
exit;
} else if ($cost <= 1) {
echo "Product price must be greater than 1.";
exit;
}
// Check if model and brand combination exists in the database
$mhb_rs = Database::search("SELECT * FROM `model_has_brand` WHERE `model_model_id`='" . $model . "' AND
`brand_brand_id`='" . $brand . "'");
$model_has_brand_id;
if ($mhb_rs->num_rows > 0) {
$mhb_data = $mhb_rs->fetch_assoc();
$model_has_brand_id = $mhb_data["id"];
} else {
// If not, insert a new entry into model_has_brand
Database::iud("INSERT INTO `model_has_brand`(`model_model_id`,`brand_brand_id`) VALUES
('" . $model . "','" . $brand . "')");
$model_has_brand_id = Database::$connection->insert_id;
}
// Set product status to 1 (active)
$status = 1;
// Insert new product into the database
Database::iud("INSERT INTO `products`(`price`,`description`,`title`,`category_id`,`category_category_id`,`model_has_brand_id`,`qty`,`status`,
`sellers_username`) VALUES ('" . $cost . "','" . $desc . "','" . $title . "',
'" . $category . "','" . $model . "','" . $model_has_brand_id . "','" . $qty . "','" . $status . "','" . $email . "')");
$product_id = Database::$connection->insert_id;
// Image Upload Section
// Check if images were uploaded
if (isset($_FILES) && !empty($_FILES)) {
$allowed_image_extensions = array("image/jpeg", "image/png", "image/svg+xml");
$length = sizeof($_FILES);
if ($length > 3) {
echo "Invalid Image Count. Maximum allowed: 3";
exit;
}
for ($x = 0; $x < $length; $x++) {
if (isset($_FILES["image" . $x])) {
$image_file = $_FILES["image" . $x];
$file_extension = $image_file["type"];
if (in_array($file_extension, $allowed_image_extensions)) {
// Determine new image extension based on type
$new_img_extension = "";
if ($file_extension == "image/jpeg") {
$new_img_extension = ".jpeg";
} else if ($file_extension == "image/png") {
$new_img_extension = ".png";
} else if ($file_extension == "image/svg+xml") {
$new_img_extension = ".svg";
}
// Generate unique filename and move uploaded file
$file_name = "navod//" . $title . "_" . $x . "_" . uniqid() . $new_img_extension;
if (!move_uploaded_file($image_file["tmp_name"], $file_name)) {
echo "Error uploading image.";
exit;
}
// Insert image information into product_images table
Database::iud("INSERT INTO `product_images`(`path`,`product_id`) VALUES
('" . $file_name . "','" . $product_id . "')");
} else {
echo "Invalid image type.";
exit;
}
}
}
echo "success"; // Success message if image upload is successful
} else {
echo "No images uploaded.";
}
?>