基于PHP会话的购物车问题

时间:2015-03-13 20:44:44

标签: php html mysql get shopping-cart

我有以下代码无效。我的基于会话的购物车有问题。当我添加东西时,它没有显示,我不知道为什么。

main.php文件,前几行,他们收到信息来添加东西到购物车:

//Start the session
session_start();

//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }


//Add an item only if we have the required pices of information: name, qty
if (isset($_GET['name']) && isset($_GET['qty']) && isset($_GET['add'])){
    //Adding an Item
    //Store it in a Array
    $ITEM = array(
        //Item name     
        'name' => $_GET['name'], 
        //Item Price
        //'price' => $_GET['price'], 
        //Qty wanted of item
        'qty' => $_GET['qty']       
        );

    //Add this item to the shopping cart
    $_SESSION['SHOPPING_CART'][] =  $ITEM;
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
    //Remove the item from the cart
    unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    //Re-organize the cart
    //array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');

}
else if (isset($_GET['empty'])){
    //Clear Cart by destroying all the data in the session
    session_destroy();
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');

}
else if (isset($_POST['update'])) {
    //Updates Qty for all items
    foreach ($_POST['items_qty'] as $itemID => $qty) {
        //If the Qty is "0" remove it from the cart
        if ($qty == 0) {
            //Remove it from the cart
            unset($_SESSION['SHOPPING_CART'][$itemID]);
        }
        else if($qty >= 1) {
            //Update to the new Qty
            $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
        }
    }
    //Clear the POST variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');
} 

?>

这是应该显示购物车内容的div,但不是,它是空的,不知道为什么。它也在main.php里面

<div id="cart">
<form action="" method="post" name="shoppingcart" style="font-size:12px;">
    <?php 
    //We want to include the shopping cart in the email
    ob_start();
    ?>
    <table width="500" border="1" id="searchTbl">
      <tr>
        <th scope="col">Edit</th>
        <th scope="col">Description</th>
        <th scope="col">Quantity</th>
      </tr>

        <?php 
        //Print all the items in the shopping cart
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td><a href="?remove=<?php echo $itemNumber; ?>">Löschen</a></td>
            <td><?php echo $item['name']; ?></td>
            <!-- <td><?php echo $item['price']; ?></td> -->
            <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
            <!-- <td><?php echo $item['qty'] * $item['price']; ?></td>   -->      
        </tr>
        <?php
        }
        ?>
    </table>
    <?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
    <p>
      <label>
      <input type="submit" name="update" id="update" value="Update Cart" />
      </label>
    </p>
</form>
<p><a href="?empty">Empty Cart</a></p>
</div>

将项目添加到购物车的链接如下所示, $ description从MySQL变量中正确获取,因此可以正常工作。

<a rel='facebox' href='editqty.php?name={$description}'><img src='img/mail.png' width='25' ></a>

editqty.php是一个弹出窗口,您可以在其中输入所需的金额,然后单击“发送”,然后引用main.php文件,该文件应通过执行$ _GET来获取值:

<?php
try {
$id = $_GET['name'];
}

catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>


<form method="post" action="index.php">
<fieldset>
<legend>Amount?</legend>
<table>
<tr>
    <td>Amount:</td>
    <td><input type="text"  name="qty" required value="" autocomplete="off"></td>
    <td><input type="hidden" name="name" value="<?php echo $id; ?>" autocomplete="off"></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</fieldset>
</form>

现在我知道名称的值从每个GET的main.php到editqty.php,然后在隐藏值中使用,并添加了qty值,然后将这两个值发送回main.php,有效,我已经测试了使用篡改数据,所以我可以看到两个变量填充信息通过main.php。现在main.php中的第一个脚本部分实际上似乎做了一些事情,它指的是正确的#cart并重新加载页面,但是那个表中没有内容可以显示...

我做错了什么?

与往常一样,在此先感谢,每一个提示都非常受欢迎!

修改 我刚刚发现如果我将URL更改为:

<a href='main.php?name={$description}&qty=5&add=foobar'><img src='img/mail.png' width='25' ></a>

它的工作方式就像一个魅力,所以我想有一些事要做,我正在调用另一个PHP脚本,为我添加数量,因为我没有输入我想要的main.php中的数量用外部PHP做到这一点......我必须保持会话活着吗?

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。我将把这个问题遗留给其他有这个问题的人。

这是我使用POST为表单发送我的vars但是main.php正在等待GET的一个基本错误。所以我只是将表单方法改为GET,然后就可以了。

干杯

相关问题