source: Innova/CreateOrder.php@ 057badc

Last change on this file since 057badc was 057badc, checked in by Vlado 222039 <vlado.popovski@…>, 2 months ago

Adding code

  • Property mode set to 100644
File size: 2.2 KB
Line 
1<?php
2
3 session_start();
4
5 require './connect.php';
6
7 if(!isset($_SESSION['user_ID']) ) {
8 header("Location: ./Log In.php");
9 die();
10 }
11
12 if(!isset($_POST['user_id'])) {
13 header("Location: ./Cart.php");
14 die();
15 }
16
17 try {
18 $personal_data = mysqli_query($conn, "SELECT * FROM users_information WHERE user_id = {$_SESSION['user_ID']};");
19
20 if(mysqli_num_rows($personal_data) <= 0) {
21 header("Location: ./EditProfile.php");
22 die();
23 }
24 else {
25 $personal_data = $personal_data->fetch_assoc();
26 }
27
28 $order_items = mysqli_query($conn, "SELECT * FROM cart WHERE user_id = {$_SESSION['user_ID']};");
29 $order_price = 2;
30
31 foreach($order_items as $item) {
32 $item_information = mysqli_query($conn, "SELECT * FROM products WHERE product_id = {$item['product_id']};")->fetch_assoc();
33 $order_price = $order_price + (($item_information['price'] - ($item_information['price']*$item_information['discount']/100.0))*$item['quantity']);
34 }
35
36 mysqli_query($conn, "insert into orders(user_id, order_date, total_sum, status, city, postal_code, address, phone_number, name, surname)".
37 "values({$_SESSION['user_ID']}, CURDATE(), {$order_price}, 'p', '{$personal_data['city']}', {$personal_data['postal_code']}, '{$personal_data['address']}', ".
38 "'{$personal_data['phone_number']}', '{$personal_data['name']}', '{$personal_data['surname']}');");
39
40 $order_id = $conn->insert_id;
41
42 foreach($order_items as $item) {
43 $item_information = mysqli_query($conn, "SELECT * FROM products WHERE product_id = {$item['product_id']};")->fetch_assoc();
44 $item_price = $item_information['price'] - ($item_information['price']*$item_information['discount']/100.0);
45 mysqli_query($conn, "INSERT INTO order_item(order_id, product_id, quantity, price) VALUES ($order_id, {$item_information['product_id']}, {$item['quantity']}, {$item_price});");
46 }
47
48 mysqli_query($conn, "DELETE FROM cart WHERE user_id = {$_SESSION['user_ID']};");
49
50 header("Location: ./Profile.php");
51 } catch(Exception $e) {
52
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68?>
Note: See TracBrowser for help on using the repository browser.