Ejemplo n.º 1
0
<?php

//dont forget to change default session timer for session variables in php.ini
session_start();
include 'Database.class.php';
//Check if the get value exists, else go back home.
if (isset($_GET['productId'])) {
    $productId = $_GET['productId'];
    if (isset($_SESSION['numOfCartItems'])) {
        //check if exists in database
        checkProduct($productId);
        $_SESSION['numOfCartItems']++;
    } else {
        //check if exists in database
        checkProduct($productId);
        $_SESSION['numOfCartItems'] = 1;
    }
    //redirect home when finished.  Will need to change this eventually to just go back to last page
    header("location:../home");
} else {
    //if a productID isn't set in get, redirect home
    header("location:../home");
}
function checkProduct($productId)
{
    //connect to db
    $dbh = Database::getDB();
    //prepare qry
    $check = $dbh->prepare("SELECT iid, price, quantity\r\n\t\t\t\t\t\t\t\tFROM inventory\r\n\t\t\t\t\t\t\t\tWHERE iid=:iid");
    //Bind the values
    $check->bindParam(':iid', $productId, PDO::PARAM_INT);
Ejemplo n.º 2
0
//dont forget to change default session timer for session variables in php.ini
session_start();
include 'Database.class.php';
//Check if the get value exists, else go back home.
if (isset($_POST['productId']) && isset($_POST['quantity'])) {
    //Filter numbers for now.  Might have to change if not only numbers
    $productId = filter_var($_POST['productId'], FILTER_SANITIZE_NUMBER_INT);
    $quantity = filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT);
    if (isset($_SESSION['numOfCartItems'])) {
        //check if exists in database
        checkProduct($productId, $quantity);
        $_SESSION['numOfCartItems'] += $quantity;
    } else {
        //check if exists in database
        checkProduct($productId, $quantity);
        $_SESSION['numOfCartItems'] = $quantity;
    }
    //grab previous page
    $previous = "javascript:history.go(-1)";
    if (isset($_SERVER['HTTP_REFERER'])) {
        $previous = $_SERVER['HTTP_REFERER'];
    }
    //redirect to continue page, they decide if they want to order or go back.
    $_SESSION['itemsAddedFlag'] = 1;
    $_SESSION['previousAddress'] = $previous;
    header("location:../continue");
} else {
    //if a productID and quantity isn't set in get, redirect home
    header("location:../home");
}