php - 循环完成后重定向成功
问题描述
我有一个购物车会话,它通过循环完全插入数据库,一旦我重定向到会话,它只会从会话中的多个产品中插入一个产品。
for($i = 0; $i < count($_SESSION['shopping_cart']); $i++){
$title = $_POST['title'][$i];
$quantity = $_POST['quantity'][$i];
$total = $_POST['total'][$i];
$sql = "INSERT INTO orders (title, quantity, total, paid, created_at, updated_at)";
$sql .= " VALUES ( '";
$sql .= $title . "', '";
$sql .= $quantity . "', '";
$sql .= $total . "', '";
$sql .= 0 . "', '";
$sql .= $timestamp . "', '";
$sql .= $timestamp . "')";
$result = $database->query($sql);
if($result){
//success
echo 'yes';
//redirect_to('order_summary.php');
}else{
echo 'no';
//failure
}
}
这是下表,我有点困惑为什么每当我进行页面重定向时,for循环并没有完全插入数据库中,但是每次我删除重定向时,它都会将所有细节完全循环到数据库中。
<table class="table table-striped">
<tr>
<th colspan="7"><h3 class="text-center">Order details</h3></th>
</tr>
<tr class="bg bg-success">
<th>Title</th>
<th>Slug</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Actions</th>
</tr>
<?php
if(!empty($_SESSION['shopping_cart']));
$total = 0;
foreach($_SESSION['shopping_cart'] as $key => $product):
?>
<form method="post" action="cart5.php" class="form-horizontal">
<tr>
<td class="text-info"><input type="text" readonly class="form-control" name="title[]" value="<?php echo $product['title']; ?>"/></td>
<td><input type="text" readonly class="form-control" name="price[]" value="N<?php echo $product['price']; ?>"/></td>
<td class="text-center text-info"><input type="text" readonly class="form-control" name="quantity[]" value="<?php echo $product['quantity']; ?>"/></td>
<td><input type="text" readonly class="form-control" name="total[]" value="N<?php echo number_format($product['quantity'] * $product['price'], 2); ?>"/></td>
</tr>
<?php
$total = $total + ($product['quantity'] * $product['price']);
endforeach;
?>
<tr>
<td class="bg bg-success" colspan="7" align="center"><b>SubTotal = N</b><input type="text" readonly multiple class="form-control" name="subTotal[]" value="<?php echo number_format($total, 2); ?>"/></td>
</tr>
<tr>
<td colspan="7">
<?php
if (isset($_SESSION['shopping_cart'])):
if (count($_SESSION['shopping_cart']) > 0):
?>
<input type="submit" name="submit" class="btn btn-success text-right" value="Checkout" />
<?php endif; endif; ?>
</td>
</tr>
</table>
解决方案
问题是您在第一次成功后在循环中重定向......
for($i = 0; $i < count($_SESSION['shopping_cart']); $i++){
// ....
if($result){
//success
echo 'yes';
//redirect_to('order_summary.php');
}else{
echo 'no';
//failure
}
}
您需要做的是仅在失败或循环完成时重定向......
for($i = 0; $i < count($_SESSION['shopping_cart']); $i++){
// ....
if(!$result){
echo 'no';
//failure
redirect_to('failed.php');
}
}
redirect_to('order_summary.php');
或者使用一个标志来标记循环后要做什么......
$success = true;
for($i = 0; $i < count($_SESSION['shopping_cart']); $i++){
// ....
if(!$result){
echo 'no';
//failure
$success = false;
}
}
if ( $success ) {
redirect_to('order_summary.php');
}
else {
redirect_to('failed.php');
}
这是基于redirect_to()
输出标头并自动调用exit
将停止脚本的假设。
所有这些都有可能会留下一半的订单,这取决于您是否要将其全部包装在交易中。
推荐阅读
- angular - Angular 7 - 轮询时闪烁的图像
- python-3.x - python循环的问题
- ada - Ada - SHA3 实现的合理代码结构
- matlab - 变量 YYY 原本保存为 XXX 无法实例化为对象,将作为 uint32 读入
- java - 在 Ehcache 3.x 中获取缓存条目的年龄
- google-apps-script - 在 Google Apps 脚本中使用机密模式发送电子邮件
- python - 在重新运行代码时,在 python 中存储类的实例时,最佳实践是什么?
- javascript - 使用 isset() 进行用户验证
- comsol - 如何获取 comsol 许可证文件和用于下载 comsol multiphysics 的试用密码
- typescript - Typescript Conditional Types 结合映射类型文档示例说明