This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateCart.php
93 lines (72 loc) · 2.2 KB
/
updateCart.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
<?php include "session.php"; ?>
<?php include_once "./models/DatabaseLink.php"; ?>
<?php
/*
*updateCart.php
*Checks the users cart, and updates the values to match the values inputed by the user into the input boxes.
*/
//redirect
header("location: mycart.php");
/* Connect to database */
$db = new DatabaseLink();
$con = $db->connection;
$query = "";
$row = array();
//passed in variable
$curU = $_SESSION['accountId'];
//check if item exists already in users cart, if not, increment.
$query = ("SELECT product_id FROM `cart_items` WHERE account_id = '$curU' " );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$cart = array();
$size = 0;
$worked = 0;
/*
*If the user has a cart, get the size
*scan through the cart, and if the user inputed 0, remove from cart
*if the user tried to input more quanity than stock, set the the max amount allowed
*set the cart item to the input value.
*/
if($row[0] != NULL)
{
$cart[$size] = $row[0];
$size++;
while($row = mysql_fetch_array($result))
{
$cart[$size] = $row[0];
$size++;
}
for($i = 0; $i < $size; $i++)
{
$query = ("SELECT amount FROM `cart_items` WHERE account_id = '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$quanity = $_POST[$cart[$i]];
$query = ("SELECT name, price, inventory FROM `products` WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$name = $row[0];
$price = $row[1];
$quanityLeft = $row[2];
$a = 0;
if($quanityLeft == 0 or $quanityLeft < $quanity)
{
$a = $quanityLeft;
}
else
{
$a = $_POST[$cart[$i]];
}
if($a > 0)
{
$query = ("UPDATE `cart_items` SET amount = $a WHERE account_id= '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
}
else
{
$query = ("DELETE FROM `cart_items` WHERE account_id= '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
}
}
}
?>