简体   繁体   English

循环完成后成功重定向

[英]Redirect successfully after loop is completed

i have a shopping cart session which completely inserts into the database through a loop, once i make a redirect to the session, it inserts only one product out of the multiple products in the session. 我有一个购物车会话,它通过循环完全插入数据库中,一旦我重定向到该会话,它只会在该会话的多个产品中插入一个产品。

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

    }
        }

This is the table below, i am abit confused as to why whenever i make the page redirect, the for loop is not totally inserted in the database, but each time i remove the redirect, it loops completely with all the details into the database. 这是下表,我对为什么每次进行页面重定向时,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>

The problem is that you redirect in the loop after the first success... 问题是您在第一次成功后会在循环中重定向...

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

    // ....
    if($result){
        //success
        echo 'yes';
        //redirect_to('order_summary.php');
    }else{
        echo 'no';
        //failure

    }
}

What you need to do is redirect only either on failure or when the loop has finished... 您需要做的是仅在失败或循环完成时重定向...

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

    // ....
    if(!$result){
        echo 'no';
        //failure
        redirect_to('failed.php');

    }
}
redirect_to('order_summary.php');

Or use a flag which flags what to do after the loop... 或者使用标记来标记循环后的操作...

$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');
}

This is based on the assumption that redirect_to() is outputting the header and automatically calling exit which will stop the script. 这是基于以下假设: redirect_to()正在输出标头并自动调用exit ,这将停止脚本。

All these have the possibility that they will leave an order half inserted, depending on how important that is may dictate if you want to wrap it all in a transaction or not. 所有这些都有可能使订单保留一半插入,这取决于您是否要将其全部包装在事务中而决定的重要性。

Your logic is telling it to redirect after any successful insert. 您的逻辑是告诉它在成功插入后进行重定向。 Hence, first run through the loop it works and it redirects. 因此,首先运行循环,然后重新定向。

Also, you can't send header() s after starting any output to the client - the echo 'yes' may give you problems. 另外,在开始将任何输出发送到客户端之后,您将无法发送header()echo 'yes'可能会给您带来问题。 I'd change it to set/track a boolean and after the loop is complete do something. 我将其更改为设置/跟踪布尔值,并在循环完成后执行一些操作。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM