【发布时间】:2015-05-16 11:46:48
【问题描述】:
我有以下代码不起作用。我的基于会话的购物车有问题。当我添加东西时,它不显示,我不知道为什么。
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 来做到这一点...我是否必须保持会话处于活动状态或其他什么?
【问题讨论】:
标签: php html mysql get shopping-cart