<?php

include('../../conf/conn.php');

$query = 'select * from clients where username="' . mysqlSecurity($conn, $_GET["username"]) . '"';
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
    $id = mysqlSecurity($conn, $_GET["id"]);
    $value = mysqlSecurity($conn, $_GET["value"]);
    $username = mysqlSecurity($conn, $_GET["username"]);
    switch ($value) {
        case "+":
            $query = 'select * from cart where username="' . $username . '" and id=' . $id;
            $result = mysqli_query($conn, $query);
            $row = mysqli_fetch_assoc($result);
            $row["amount"] ++;
            $query = "UPDATE `cart` SET `amount` = '" . $row["amount"] . "' WHERE `cart`.`record` = " . $row["record"];
            $result = mysqli_query($conn, $query);
            echo $row["amount"];
            break;
        case "-";
            $query = 'select * from cart where username="' . $username . '" and id=' . $id;
            $result = mysqli_query($conn, $query);
            $row = mysqli_fetch_assoc($result);

            if ($row["amount"] != 1) {
                $row["amount"] --;
            }

            $query = "UPDATE `cart` SET `amount` = '" . $row["amount"] . "' WHERE `cart`.`record` = " . $row["record"];
            $result = mysqli_query($conn, $query);
            echo $row["amount"];
            break;
        default:
            $query = 'select * from cart where username="' . $username . '" and id=' . $id;
            $result = mysqli_query($conn, $query);
            $row = mysqli_fetch_assoc($result);

            if ($value >= 1) {
                $query = "UPDATE `cart` SET `amount` = '" . $value . "' WHERE `cart`.`record` = " . $row["record"];
                $result = mysqli_query($conn, $query);
                echo $value;
            } else {
                echo $row["amount"];
            }

            break;
    }
} 