循环完成后成功重定向

时间:2018-11-17 14:15:04

标签: php mysqli

我有一个购物车会话,该会话完全通过循环插入数据库,一旦我重定向到该会话,它只会在该会话的多个产品中插入一个产品。

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>

2 个答案:

答案 0 :(得分:0)

问题是您在第一次成功后在循环中重定向...

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,这将停止脚本。

所有这些都有可能使订单保留一半插入,这取决于您是否要将其全部包装在事务中可能有多重要。

答案 1 :(得分:0)

您的逻辑告诉它在任何成功插入之后重定向。因此,首先运行循环,然后重新定向。

此外,在开始将任何输出发送到客户端之后,您将无法发送header()-echo 'yes'可能会给您带来麻烦。我将其更改为设置/跟踪布尔值,并在循环完成后执行一些操作。

for($i = 0; $i < count($_SESSION['shopping_cart']); $i++){  
    $title          = $_POST['title'][$i];
    $quantity       = $_POST['quantity'][$i];
    $total          = $_POST['total'][$i];

    $success=true; // assume it works
    $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){
        //  failure do something like trapping the sql error, 
        //  or recording a message in an array
        $success=false;
    }
}
if($success){
     // it all worked!
     // do your redirect here
}else{
    // something failed
    // do your error output, warning to user, whatever here
}