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
/
removeFromCart.php
79 lines (62 loc) · 1.63 KB
/
removeFromCart.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
<?php include "session.php"; ?>
<?php include_once "./models/DatabaseLink.php"; ?>
<?php
/*
*RemoveFromCart.php
*removes an item from the cart
*
*/
//redirect
header("location: mycart.php");
//passed in variables
$pId = $_GET['id'];
$curU = $_SESSION['accountId'];
/* Connect to database */
$db = new DatabaseLink();
$con = $db->connection;
$query = "";
$row = array();
$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 user has a card, get the size of the cart,
if the item matches the item to be removed,
decrement it by one,
if the item amount = 0,
remove it completely.
*/
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++)
{
if($cart[$i] == $pId)
{
$query = ("SELECT amount FROM `cart_items` WHERE account_id = '$curU' AND product_id=" . $pId );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$a = $row[0] - 1;
if($a > 0)
{
$query = ("UPDATE `cart_items` SET amount = $a WHERE account_id= '$curU' AND product_id=" . $pId );
$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=" . $pId );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
}
}
}
}
?>