-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.php
113 lines (90 loc) · 3.41 KB
/
actions.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
<?php
include "conn.php";
$nameErr = $descriptionErr = $brandErr = $priceErr = "";
$name = $description = $brand = $price = "";
// add new product
if (($_SERVER["REQUEST_METHOD"] === "POST")) {
if(isset($_POST['add'])){
if (empty($_POST["name"])) {
$nameErr = "هذا الحقل مطلوب";
} elseif (is_numeric($_POST["name"])) {
$nameErr = "يجب ان يكون الحقل نص";
} else {
$name = check_input($_POST["name"]);
}
if (empty($_POST["description"])) {
$descriptionErr = "هذا الحقل مطلوب";
} elseif (is_numeric($_POST["description"])) {
$descriptionErr = "يجب ان يكون الحقل نص";
} else {
$description = check_input($_POST["description"]);
}
if (empty($_POST["brand"])) {
$brandErr = "هذا الحقل مطلوب";
} elseif (is_numeric($_POST["brand"])) {
$brandErr = "يجب ان يكون الحقل رقم";
} else {
$brand = check_input($_POST["brand"]);
}
if (empty($_POST["price"])) {
$priceErr = "هذا الحقل مطلوب";
} elseif (!is_numeric($_POST["price"])) {
$priceErr = "يجب ان يكون الحقل رقم";
} else {
$price = check_input($_POST["price"]);
}
if ($name !== '' && $description !== '' && $brand !== '' && $price !== '') {
$add = "INSERT INTO PRODUCTS(name, description, brand, price) VALUES('$name' , '$description' ,'$brand' , $price)";
if (mysqli_query($conn, $add)) {
header("location:index.php");
}
}
}
# Update Quantity from cart table
if (isset($_POST['update'])) {
if (!empty($_POST["id"]) && is_numeric($_POST["id"])) {
$id = check_input($_POST["id"]);
}
if (!empty($_POST["quantity"]) && is_numeric($_POST["quantity"])) {
$quantity = check_input($_POST["quantity"]);
}
if ($quantity !== '') {
$update = "UPDATE cart SET quantity = $quantity where id = $id";
if (mysqli_query($conn, $update)) {
header("location:cart.php");
}
}else{
header("location:editProductOnCart.php?id=".$id);
}
}
}
if (($_SERVER["REQUEST_METHOD"] === "GET") ) {
if(isset($_GET['product_id']) && is_numeric($_GET["product_id"])) {
$product_id = $_GET["product_id"];
//get product name where id = $product_id
$data = "SELECT name FROM products where id = $product_id";
$result = mysqli_query($conn, $data);
$row= mysqli_fetch_assoc($result);
$product_name = $row['name'];
$quantity = 1;
$add = "INSERT INTO CART(product_id,product_name , quantity)
VALUES($product_id , '$product_name' , $quantity)";
if(mysqli_query($conn ,$add)){
header("location:cart.php");
}
}
if(isset($_GET['delete_product']) && is_numeric($_GET["delete_product"])) {
$cart_id = $_GET["delete_product"];
$delete = "DELETE FROM cart where id = $cart_id";
if(mysqli_query($conn , $delete)){
header("location:cart.php");
}
}
}
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}