-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_db.php
87 lines (74 loc) · 3.25 KB
/
update_db.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
<!DOCTYPE html>
<?php
require_once('functions.php');
/* Get attributes */
$con = mysqli_connect("localhost", "root", "", "nottingham_forest");
$pid = $_GET['pid'];
$mode = $_GET['mode'];
$product_name = $_GET['product_name'];
$price = $_GET['price'];
$category_id = $_GET['category_id'];
$image_url = $_GET['image_url'];
$description = $_GET['description'];
$traits = $_GET['traits'];
$traits = explode("\n", $traits);
/* Delete product */
if($mode == "del") {
$query = "SELECT * FROM order_items WHERE order_items.product_id = $pid;";
$results = mysqli_query($con, $query);
$order_items = mysqli_fetch_array($results);
if(!is_null($order_items)) AlertNRedirect("Cannot delete product. - Related order exists.");
else {
$query = "DELETE FROM products WHERE products.product_id = $pid";
$results = mysqli_query($con, $query);
if($results) AlertNRedirect("Sussesfully deleted product.");
else AlertNRedirect("Deletion failed.");
}
/* Add or update product */
} else if($mode == "update") {
$traits_array = mysqli_query($con, "SELECT * FROM product_traits;");
/* Make traits dictionary */
$traits_dic = array();
while($trait = mysqli_fetch_array($traits_array)) $traits_dic[$trait[1]] = $trait[0];
/* Convert trait names to id */
$trait_count = count($traits);
$traits_values = array();
for($i = 0; $i < $trait_count; $i++) {
$temp = explode(":", $traits[$i]);
if(substr($temp[1], -1) == chr(13)) $temp[1] = substr($temp[1], 0, -1);
array_push($traits_values, array($traits_dic[$temp[0]], $temp[1]));
}
/* Insert new product */
if($pid == 0) {
$query_0 = "INSERT INTO products (product_name, category_id, description, price, image_url)
VALUES ('$product_name', $category_id, '$description', $price, '$image_url');";
$results_0 = mysqli_query($con, $query_0);
$query_1 = "SET @product_id = LAST_INSERT_ID();";
$results_1 = mysqli_query($con, $query_1);
$query_2 = "INSERT INTO product_trait_values (product_id, trait_id, value)
VALUES
";
for($i = 0; $i < $trait_count; $i++) $query_2 = $query_2 . "(@product_id, " . $traits_values[$i][0] . ", '" . $traits_values[$i][1] . "'), ";
$query_2 = substr($query_2, 0, -2) . ";";
$results_2 = mysqli_query($con, $query_2);
if($results_0 && $results_1 && $results_2) AlertNRedirect("Sussesfully added new product.");
else AlertNRedirect("Product add failed.");
/* Update product */
} else {
$query_0 = "UPDATE products
SET product_name = '$product_name', category_id = $category_id, description = '$description', price = $price, image_url = '$image_url'
WHERE product_id = $pid;";
$results_0 = mysqli_query($con, $query_0);
$query_1 = "DELETE FROM product_trait_values WHERE product_id = $pid;";
$results_1 = mysqli_query($con, $query_1);
$query_2 = "INSERT INTO product_trait_values (product_id, trait_id, value)
VALUES
";
for($i = 0; $i < $trait_count; $i++) $query_2 = $query_2 . "($pid, " . $traits_values[$i][0] . ", '" . $traits_values[$i][1] . "'), ";
$query_2 = substr($query_2, 0, -2) . ";";
$results_2 = mysqli_query($con, $query_2);
if($results_0 && $results_1 && $results_2) AlertNRedirect("Sussesfully updated product.");
else AlertNRedirect("Product update failed.");
}
}
?>