-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckoutCart.php
54 lines (42 loc) · 2.18 KB
/
checkoutCart.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
<?php
require './config/connection.php';
$order_data = json_decode(file_get_contents("php://input"));
function debug_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
};
$arrayProductsId = array();
$i = 0;
//Insert all products from the order
foreach ($order_data->products as $product) {
$songId = $product->product_id;
$productName = $product->product_name;
$productPrice = $product->product_price;
$productQuantity = $product->product_quantity;
$sql = "INSERT INTO product (songId, product_name, product_price, product_quantity) VALUES ('$songId','$productName','$productPrice','$productQuantity')";
$result = mysqli_query($mysqli, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($mysqli), E_USER_ERROR);
$currentProductId = mysqli_insert_id($mysqli);
array_push($arrayProductsId, $currentProductId);
$i++;
}
//Create a new order
$userId = $order_data->userData[0]->id;
$userName = $order_data->userData[0]->userName;
$totalToPay = $order_data->userData[0]->totalPayment;
$status = "Canceled";
$sql = "INSERT INTO orders (userId, userName, status, total_to_pay) VALUES ('$userId','$userName','$status','$totalToPay')";
$result = mysqli_query($mysqli, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($mysqli), E_USER_ERROR);
$orderId = mysqli_insert_id($mysqli);
//insert product and order in order_has_product table which relates them
foreach ($arrayProductsId as $productId) {
$currrentProductId = $productId;
$sql = "INSERT INTO order_has_products (idOrder, idProduct) VALUES ('$orderId','$productId')";
$result = mysqli_query($mysqli, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($mysqli), E_USER_ERROR);
$currentProductId = mysqli_insert_id($mysqli);
array_push($arrayProductsId, $currentProductId);
}
session_start();
$_SESSION['orderId'] = $orderId;
?>