如何将数据库中的产品传递给PHP中的会话变量

时间:2015-04-20 01:29:38

标签: php sql

我正在尝试用PHP创建购物车。我理解如何使用硬编码产品的会话,但我很难尝试从SQL数据库中检索产品。

这是我到目前为止所做的 CART.php:

function add_item($key, $quantity) {
    global $products;
    if ($quantity < 1) return;
    // If item already exists in cart, update quantity
    if (isset($_SESSION['cart'][$key])) {
        $quantity += $_SESSION['cart'][$key]['qty'];
        update_item($key, $quantity);
        return;
    }

    // Add item
    $cost = $products[$key]['cost'];
    $total = $cost * $quantity;
    $item = array(
        'name' => $products[$key]['name'],
        'cost' => $cost,
        'qty'  => $quantity,
        'total' => $total
    );
    $_SESSION['cart'][$key] = $item;
}

CART_VIEW.php

 <h1>Your Cart</h1>
        <?php if (empty($_SESSION['cart']) || count($_SESSION['cart']) == 0) : ?>
            <p>There are no items in your cart.</p>
        <?php else: ?>
            <form action="." method="post">
            <input type="hidden" name="action" value="update">
            <table>
                <tr id="cart_header">
                    <th class="left">Item</th>
                    <th class="right">Item Cost</th>
                    <th class="right">Quantity</th>
                    <th class="right">Item Total</th>
                </tr>
            <?php foreach( $_SESSION['cart'] as $key => $item ) :
                $cost  = number_format($item['cost'],  2);
                $total = number_format($item['total'], 2);
            ?>
                <tr>
                    <td>
                        <?php echo $item['name']; ?>
                    </td>
                    <td class="right">
                        $<?php echo $cost; ?>
                    </td>
                    <td class="right">
                        <input type="text" class="cart_qty"
                            name="newqty[<?php echo $key; ?>]"
                            value="<?php echo $item['qty']; ?>">
                    </td>
                    <td class="right">
                        $<?php echo $total; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
                <tr id="cart_footer">
                    <td colspan="3"><b>Subtotal</b></td>
                <td>$<?php echo get_subtotal(); ?></td>
                </tr>
                <tr>
                    <td colspan="4" class="right">
                        <input type="submit" value="Update Cart">
                    </td>

ADD_ITEM.php

<h1>Add Item</h1>
        <form action="../cart/index.php" method="post">
            <input type="hidden" name="action" value="add">

            <label>Name:</label>
            <section>
        <h1><?php echo $name; ?></h1>
        <div id="left_column">
            <p>

            </p>
            <select name="productkey">
            <?php foreach($products as $key => $product) :

                $cost = number_format($product['cost'], 2);
                $name = $product['name'];
                $item = $name . ' ($' . $cost . ')';
            ?>
                <option value="<?php echo $key; ?>">
                    <?php echo $item; ?>
                </option>
            <?php endforeach; ?>
            </select><br>

            <label>Quantity:</label>
            <select name="itemqty">
            <?php for($i = 1; $i <= 10; $i++) : ?>
                <option value="<?php echo $i; ?>">
                    <?php echo $i; ?>
                </option>
            <?php endfor; ?>
            </select><br>

            <label>&nbsp;</label>
            <input type="submit" value="Add Item">
        </form>

的index.php

<?php

$lifetime = 60 * 60 * 24 * 365 * 4;    // 3 years in seconds
session_set_cookie_params($lifetime, '/');
session_start();

// Create a cart array if needed
if (empty($_SESSION['cart'])) { $_SESSION['cart'] = array(); }

// Create a table of products
$products = array(); //hard coded products <------------
$products['MMS-1754'] = array('name' => 'PC', 'cost' => '549.50');


// Include cart functions
require_once('cart.php');

// Get the action to perform
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
    $action = filter_input(INPUT_GET, 'action');
    if ($action === NULL) {
        $action = 'show_add_item';
    }
}

// Add or update cart as needed
switch($action) {
    case 'add':
        $product_key = filter_input(INPUT_POST, 'productkey');
        $item_qty = filter_input(INPUT_POST, 'itemqty');
        add_item($product_key, $item_qty);
        include('cart_view.php');
        break;
    case 'show_cart':
        include('cart_view.php');
        break;
    case 'show_add_item':
        include('add_item_view.php');
        break;
    case 'end_session':
        // Clear session data from memory
        $_SESSION = array();

        // Clean up session ID
        session_destroy(); ...

products_db

<?php



function get_products() {
    global $db;
    $query = 'SELECT productID, categoryID, productName,description,price,imageFile
             FROM products
             ORDER BY productID' ;
    $statement = $db->prepare($query);
    $statement->execute();    
    $products = $statement->fetchAll();
    $statement->closeCursor();
    return $products;
}
function get_products_by_name($product_id) {
    global $db;
    $query = 'SELECT * FROM products
              WHERE products.productName = :product_id';

    $statement = $db->prepare($query);
    $statement->bindValue(":product_id", $product_id);
    $statement->execute();
    $product_name = $statement->fetchAll();
    $statement->closeCursor();
    return $product_name;
}

function get_products_by_category($category_id) {
    global $db;
    $query = 'SELECT * FROM products
              WHERE products.categoryID = :category_id
              ORDER BY productID';
    $statement = $db->prepare($query);
    $statement->bindValue(":category_id", $category_id);
    $statement->execute();
    $product = $statement->fetchAll();
    $statement->closeCursor();
    return $product;
}

function get_product($product_id) {
    global $db;
    $query = 'SELECT * FROM products
              WHERE productID = :product_id';
    $statement = $db->prepare($query);
    $statement->bindValue(":product_id", $product_id);
    $statement->execute();
    $product = $statement->fetch();
    $statement->closeCursor();
    return $product;
}

我需要不同的功能吗?我尝试使用产品ID并使用GET数组,但会话将打印为null(使用var-dump进行检查)。

0 个答案:

没有答案