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

7d7tgy0s  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(292)

我在这里尝试的是通过smtp phpmailer插入和发送订单数。我使用foreach将订单数据分离为键值对并插入数据库。
您可以在这里看到:

一旦执行成功,现在我想发送一封邮件给业主与订单的细节,这是刚刚插入。只是为了测试,我只选择productid作为订单细节。
这是刚到的邮件。

问题
我看到的是数据库已经插入了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;
                            }
                        }

            }
twh00eeo

twh00eeo1#

每次你打电话 $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>';
pepwfjgg

pepwfjgg2#

我找到的解决方案。
所以,我以前遇到的问题是 sendmail() 内部函数 foreach() 最重要的是,只显示一次消息,我使用if条件来阻止 $row['productid'] 只显示一个productid,结果用productid恢复邮件。
相反,我必须这样做:

if(!empty($_SESSION["shopping_cart"]) && $_SESSION["shopping_cart"]!=='')
{
 $shown = 0;
 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())
                        {

                                $body .=  '<p><b>Product ID:</b>&nbsp;&nbsp;&nbsp;'.$val['product_id'].'</p>';   
                                $body .= '<p><b>Discussion Name:</b>&nbsp;&nbsp;&nbsp;'.$val['product_quantity'].'</p>'; 
                                echo 'success';     

                        }else
                        {

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

            }

      $mail->msgHTML($body);
      $mail->Subject = 'New Order Arrived!';
      $mail->send();

}

邮件输出:

相关问题