dreamer2014520 2011-08-14 20:45 采纳率: 0%
浏览 27
已采纳

创建订单系统 - 存在一个问题,即订单中只添加了许多可能的项目中的一个

I am having a problem with a script that controls an order system that i am creating.

The system contains a search feature that queries a user supplied string against products held in a mysql table, if the string matches part of an items description, then that item is returned as a result.

Now, The problem is this.

If a user searches a string that returns say 5 results.

Then the user decides they would like to add 3 of one result and 2 of another result.

They then click on order, which submits the form.

Here is where the problem appears, as only the first result that the user has entered a quantity for is added to the order.

The order should contain two items, one with a quantity of 3 and another with a quantity of 2. It is not doing that and i have no idea what is going wrong here.

Here is the code that controls the instance of the user clicking on the order button:

if (!isset($_SESSION['order']))
{
    $_SESSION['order'] = array();
}

if (!isset($_SESSION['quantity']))
{
    $_SESSION['quantity'] = array();
}

$productQuantities=$_POST['quantity'];
if (isset($_POST['action']) and $_POST['action'] == 'Order' and $productQuantities > 0)
{
foreach($productQuantities as $productId=>$quantityS)
{
        if ($quantityS > 0) 
            {
            $_SESSION['order']["$productId"] = $productId;
            $_SESSION['quantity']["$productId"] = $quantityS;
            header('Location: .');
            exit();
            }
}
}

Can anyone see where I am making a mistake in this?

Any input would be greatly appreciated, and I will supply any additional code on request.

Thanks!

  • 写回答

1条回答 默认 最新

  • dtqqq24248 2011-08-14 20:50
    关注

    You create a loop, but in the first iteration of that loop with a quantity, you send a Location header and exit the script execution. Clearly no more products can be added to $_SESSION once you terminate the script and send the visitor to another page.

    Don't prematurely end the loop, and only send a (proper!) location header after you've finished.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?