-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderProcess.php
75 lines (69 loc) · 3.4 KB
/
orderProcess.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
<?php
require 'config.php';
session_start();
if (isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']===TRUE) {
if (isset($_POST['order_confirmation']) && $_POST['order_confirmation']=="true" && $_SESSION['cartCount']!=0) {
if ($_POST['payment_method']=="codPay") {
$sqlIdGen = "insert into orderidgenerator values()";
if ($conn->query($sqlIdGen) === TRUE) {
$sqlLastId = ("select last_insert_id() as lid");
$resultLastId=$conn->query($sqlLastId);
$id=0;
while($idRow=$resultLastId->fetch_assoc()){
$id = intval($idRow['lid']);
}
$uniqueId = 102624+$id; //102624 is the base number to be appended with OIDs
$oid = "MGO".(string)$uniqueId;
$safeDeliveryAddress = preg_replace('/[^\w,. ]/','',$_POST['delivery_address']);
$sqlOrder = "insert into productorder (uid, oid, paymentMethod, addressName) values ('".$_SESSION['userId']."', '".$oid."', 'Cash On Delivery', '".$safeDeliveryAddress."')";
if ($conn->query($sqlOrder)===TRUE) {
$sqlOrderedProducts = "select usercart.proid, quantity, sp*quantity as subprice from usercart natural join productseller where uid='".$_SESSION['userId']."'";
$resultOrderedProducts=$conn->query($sqlOrderedProducts);
$sqlProductInsert="insert into productsinorder (proid, oid, quantity, totalPrice) values ";
$i=0;
while($rowOrderedProducts=$resultOrderedProducts->fetch_assoc()){
if ($i==0) {
$sqlProductInsert.="('".$rowOrderedProducts['proid']."', '".$oid."', ".$rowOrderedProducts['quantity']. ", ".$rowOrderedProducts['subprice'].")";
}else{
$sqlProductInsert.=", ('".$rowOrderedProducts['proid']."', '".$oid."', ".$rowOrderedProducts['quantity']. ", ".$rowOrderedProducts['subprice'].")";
}
$i=$i+1;
}
if ($conn->query($sqlProductInsert) === TRUE) {
//insertion successful
//Order successful
$sqlAddress="SELECT * FROM useraddress WHERE uid = '".$_SESSION['userId']."' and addressName='".$safeDeliveryAddress."'";
$resultAddress=$conn->query($sqlAddress);
$sqlInsertDeliveryAddress="";
while($row=$resultAddress->fetch_assoc()){
$sqlInsertDeliveryAddress = "insert into deliveryaddress (oid, locality, pincode, area, city, state, landmark, phone) values ('".$oid."', '".$row['locality']."', '".$row['pincode']."', '".$row['area']."', '".$row['city']."', '".$row['state']."', '".$row['landmark']."', '".$row['phone']."')";
}
if ($conn->query($sqlInsertDeliveryAddress) === TRUE) {
$_SESSION['orderConfirmed']=TRUE;
echo $oid;
}else{
//error fetching delivery address
}
$deleteProductsFromCart="delete from usercart where uid='".$_SESSION['userId']."'";
if ($conn->query($deleteProductsFromCart) === TRUE) {
//deletion from cart successful
$_SESSION['cartCount']=0;
}else{
//error deleting products from cart
}
}else{
//error inserting products
}
}else{
//error inserting order
}
} else {
//error generating orderid
}
}
} else{
//Error in page calling or cart items
}
}
$conn->close();
?>