将动态值从数据库加载到phpmailer主体中的msgHTML中

问题描述 投票:0回答:1

我在这里尝试做的是通过SMTP phpmailer插入和发送订单数。我正在使用foreach将订单数据分离为键值对,然后插入数据库。

可以在这里看到:here

一旦执行成功,现在我想发送一封邮件给所有者,其中包含刚刚插入的订单详细信息。仅出于测试目的,我仅选择productid作为订单详细信息。

这是刚收到的邮件。here

问题

[我正在寻找的是数据库已插入2个productid(26,27),但在邮件中我仅获得了productid(26),我想在邮件正文中插入动态productid。我使用while循环,但我无法达到目标。如果有人打印出我错了的地方,我将不胜感激。

PHP:

$shown = false;
        foreach($_SESSION["shopping_cart"] as $number => $val)
            {
                       // prepare and bind
                        $stmt = $conn->prepare("INSERT INTO purchase(guest_code,productid,quantity,date_purchase) VALUES (?, ?, ?, ?)");
                        $stmt->bind_param("siis",$_SESSION['CODE'],$val['product_id'],$val['product_quantity'],$current_date_time); //inserting mutiple value in db successful.

                        if($stmt->execute())
                        {
                            if(!$shown) //show msg or dedirect only once
                            {      

                                    $sql= mysqli_query($conn,"select productid from purchase where guest_code='".$_SESSION['CODE']."'"); //fetch only productid for test
                                    while ($row = mysqli_fetch_array($sql)) 
                                    {
                                          $productid = $row['productid'];
                                          $message = "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
                                          $mail->msgHTML($message); //trying to load multiple productid in $message 
                                    }    

                                        $mail->Subject = 'New Order Arrived!';
                                        if(!$mail->send()) 
                                        {
                                          $mail_error = 'error: ' . $mail->ErrorInfo;
                                          exit();
                                        } 
                                        else {
                                               //if mail sent then
                                                unset($_SESSION["shopping_cart"]); //distroy all the values in the cart
                                                //header('location:../checkout.php?er=false');
                                        }


                                $shown = true;

                            }


                        }else
                        {

                            if(!$shown) //show msg only once
                            {
                                echo 'ERROR: while placing your order. Please contact restaurant owner.';
                                $shown = true;
                            }
                        }


            }
php sql foreach phpmailer
1个回答
-1
投票

每次调用$mail->msgHTML($message);时,它替换 Body的全部内容;它不添加。我希望您按照以下方式进行操作:

$mail->Body = '<p>Your order</p><table>';
while ($row = mysqli_fetch_array($sql)) {
    $productid = $row['productid'];
    $mail->Body .= "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
}
$mail->Body .= '</table>';
© www.soinside.com 2019 - 2024. All rights reserved.