使用PHP中的foreach循环成功插入和发送邮件后进行重定向

问题描述

代码说明:

提供的代码是关于在电子商务网站上订购商品并触发PHP邮件程序通过电子邮件在特定电子邮件地址上发送购物车商品,并且还将商品同时插入数据库中。

到目前为止已完成:

我设法将记录插入数据库,并成功地将相同的记录发送/触发到邮件地址。

问题:

将购物车商品成功插入数据库并在此处触发邮件后,我想将用户重定向到checkout.PHP页...一旦成功完成所有操作,还希望取消unset($_SESSION['shopping_cart']);购物车会话。

让我们说我的购物车中有3件物品,因此此foreach循环将循环3次/相同次数,以将记录插入到DB中。在循环结束时使用break; / die();将无益。循环将在第一个循环后停止,如果使用break,则只有2个项目会添加到db中。

我将此代码设置为重定向取消设置,但是不知道在循环中将其应用于何处或如何对其进行应用。任何指导或最好的方式做到这一点。

 if($run==true)
 {
    header('location:../checkout.PHP');
    unset($_SESSION['shopping_cart']);  
     $run=false;
  }

完整代码

$run = true; 
$shown=0;
$order_prefer = 1;      
if(empty($_SESSION["shopping_cart"])|| $_SESSION["shopping_cart"] =='') {   
    echo '<div class="alert alert-danger" role="alert">
            No orders have been placed - We can only process your order with at least 1 order.</div>';
    } else {
        //include email template parts
        include "src/order_send/part_1.PHP";
        include "src/order_send/part_2.PHP";
                            
        foreach($_SESSION["shopping_cart"] as $number => $val) {
            // prepare and bind
            $stmt = $conn->prepare("INSERT INTO purchase
                                    (guest_code,productid,quantity,date_purchase,time_purchase) 
                            VALUES (?,?,?)");
            $stmt->bind_param("siiss",$_COOKIE['CODE'],$val['product_id'],$val['product_quantity'],$current_date,$current_time); 
            $stmt->store_result();  

            if($stmt->execute()) {
                $st = $conn->prepare("SELECT productid,productname 
                                        FROM product WHERE productid= ?");
                $st->bind_param("i",$val['product_id']); 
                $st->bind_result($productid,$productname);
                $st->store_result();
                $st->execute();
                while ($st->fetch()) { 
                    $json=array('pid'=>$productid,'pname'=>$productname);
                    
                    $body .='<tr>
                                <td><center><span class="badge  text-center" style="background: #65cc0b;">'.$order_prefer.'</span></center></td>
                                <td>'.$json['pid'].'</td>
                                <td>'.$json['pname'].'</td>
                                <td>'.$val['product_quantity'].'</td>
                            </tr>';
                    $order_prefer++;        
                }

                $st->close();
                $stmt->close(); 

            }else{
                //show msg only once
                if($shown==0) {
                    echo 'ERROR: while placing your order. Please contact restaurant owner';
                    header('location:../checkout.PHP?er=true');
                    $shown = 1;
                }

                $st->close();
                $stmt->close(); 

                }
                                                       
           if($run==true) {
                    header('location:../checkout.PHP');
                    unset($_SESSION['shopping_cart']);  
                    $run=false;
            }
        }


            //include email template parts
            include "src/order_send/part_3.PHP";
                                     
            $status_query ="SELECT status from mailing";
            $sq=$conn->query($status_query);
            $main_result =$sq->fetch_array();
            if(isset($main_result['status']) && $main_result['status']=='ON') 
            {
                $mails->msgHTML($body);
                $mails->Subject = 'Incoming order';
                $mails->send(); //echo 'error: ' . $mail->ErrorInfo;
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)