更新购物车的会话变量
我是 php 的新手(只使用了几个月),我正在为一个朋友构建一个非常简单的电子商务网站,我正在为其构建一个同样简单的购物车。
以下是购物车的基本思想:当前,用户浏览产品页面(基于 ProductID),选择商品的颜色(选择 ProdOptID,它对于每个商品都是唯一的),然后将该信息提交到 $ _SESSION['Cart'] 通过表单发布。然后,该信息用于访问 mysql 数据库,以提取所有其余的产品信息,然后将其显示在迷你购物车中。到目前为止,它运行良好。只有当我尝试创建评论/编辑页面时,我才会被绊倒。由于某些原因,我只能更新最后一篇。有人有任何想法或发现任何问题吗?
评论/编辑页面的代码:
<div class="ReviewCheckout_Main">
<h1 class="ReviewCheckout_Title">Review Your Cart</h1>
<div class="ReviewCheckout_Form">
<form action="/BeautifulUrns/includes/ReviewCheckout_Processing.php" method="post">
<?php
if (isset($_SESSION['Cart'])) {
foreach ($_SESSION['Cart'] as $ID => $QTY){
$Query = "SELECT *
FROM ProductsOptions
LEFT OUTER JOIN Products
ON ProductsOptions.ProductID=Products.ProductID
WHERE ProdOptID = $ID";
$Result = mysql_query($Query, $Connection);
$Row = mysql_fetch_array($Result);
echo "<input type='text' name='Edit_Qty' value=" . $QTY . " />";
echo "<b>" . $Row['ProductName'] . "</b> ";
echo "<b> - " . $Row['POName'] . "</b> ";
echo "<input type='hidden' name='Edit_ID' value='{$Row['ProductID']}' />";
echo "<input type='hidden' name='Edit_ProdOptID' value='{$Row['ProdOptID']}' />";
echo "<br />";
}
}
?>
<input type="submit" />
</form>
<a href="/BeautifulUrns/index.php">Return Home</a>
</div>
处理页面的代码:
<?php session_start();
$ID = mysql_real_escape_string((int)$_POST['Edit_ProdOptID']); // GRABS PRODOPT ID
$PRODUCTID = mysql_real_escape_string((int)$_POST['Edit_ID']); //GRABS PRODUCT ID
$QTY = mysql_real_escape_string((int)$_POST['Edit_Qty']); // GRABS QUANTITY
if(isset($ID)) {
if (isset($QTY) && $QTY > 0){
if (isset($_SESSION['Cart'])) {
$_SESSION['Cart'][$ID] = $QTY;
}
else {
$_SESSION['Cart'] = NULL;
$_SESSION['Cart'][$ID] = $QTY;
}
}
}
?>
I'm new to php (have only been playing around with it for a couple of months) and I am building a friend a really simple e-commerce website for which I am building an equally simple shopping cart.
Here's the basic idea of the cart: Currently, the user browses the products page (based off of productID), selects a color for an item (which selects ProdOptID, which is unique to every single item), and then submits that information to $_SESSION['Cart'] via posting through a form. That information is then used to access a mysql DB to pull out all the rest of the product information, which then gets displayed in a mini-cart. Up to that point, it works fine. It is only when I try to create a review/edit page where I get tripped up. For some reason, I can only update the last one. Anyone got any ideas or spot any problems?
The code for the review/edit page:
<div class="ReviewCheckout_Main">
<h1 class="ReviewCheckout_Title">Review Your Cart</h1>
<div class="ReviewCheckout_Form">
<form action="/BeautifulUrns/includes/ReviewCheckout_Processing.php" method="post">
<?php
if (isset($_SESSION['Cart'])) {
foreach ($_SESSION['Cart'] as $ID => $QTY){
$Query = "SELECT *
FROM ProductsOptions
LEFT OUTER JOIN Products
ON ProductsOptions.ProductID=Products.ProductID
WHERE ProdOptID = $ID";
$Result = mysql_query($Query, $Connection);
$Row = mysql_fetch_array($Result);
echo "<input type='text' name='Edit_Qty' value=" . $QTY . " />";
echo "<b>" . $Row['ProductName'] . "</b> ";
echo "<b> - " . $Row['POName'] . "</b> ";
echo "<input type='hidden' name='Edit_ID' value='{$Row['ProductID']}' />";
echo "<input type='hidden' name='Edit_ProdOptID' value='{$Row['ProdOptID']}' />";
echo "<br />";
}
}
?>
<input type="submit" />
</form>
<a href="/BeautifulUrns/index.php">Return Home</a>
</div>
The Code for the processing page:
<?php session_start();
$ID = mysql_real_escape_string((int)$_POST['Edit_ProdOptID']); // GRABS PRODOPT ID
$PRODUCTID = mysql_real_escape_string((int)$_POST['Edit_ID']); //GRABS PRODUCT ID
$QTY = mysql_real_escape_string((int)$_POST['Edit_Qty']); // GRABS QUANTITY
if(isset($ID)) {
if (isset($QTY) && $QTY > 0){
if (isset($_SESSION['Cart'])) {
$_SESSION['Cart'][$ID] = $QTY;
}
else {
$_SESSION['Cart'] = NULL;
$_SESSION['Cart'][$ID] = $QTY;
}
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
您可以创建临时表来存储用户选择的项目,当订单最终确定时,您可以将所有记录上传到您的查询或订单表。
使用临时表,您可以轻松更新或删除任何记录
表:temporary_order
Temp_id: $_SESSION['temp_id'] = 任意随机值;
id userid product_id quantity temp_id date
1 1 1 2 123456 2011-06-27
2 1 2 1 123456 2011-06-27
3 2 1 2 789456 2011-06-27
4 2 2 3 789456 2011-06-27
您只需要传递 temp_id 即可从临时表中获取用户选择的记录
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
看起来好像你的 html 输入名称都完全相同,因此最后一个会覆盖前一个。您应该做的是将 id 添加到输入名称,例如
然后您应该使用变量变量从每个输入字段检索数据。
http://php.net/manual/en/language.variables.variable.php
It seems as though your html input names are all exactly the same, thus the last one overides the previous ones. What you should do is add an id to the input name e.g.
Then you should use variable variables to retrieve your data from each input field.
http://php.net/manual/en/language.variables.variable.php