dpa84373 2017-03-29 10:11
浏览 30
已采纳

使用Mysqli在HTML中使用数据库表中的所有值生成动态PHP电子邮件

I'm a noob here, but I all I want to do here is to generate a PHP Mail using all the rows and their values from a Database Table using MYSQLi method.

My Table Format ndr_confirm_id | order_id (INT) | tracking_no (BIGINT) | status (0,1) | update_time Estimated Rows in the Table : 50-100

I want to generate a table within the PHP Mail which lists values from ALL rows available in the table.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$con=mysqli_connect("localhost","admin","ABC1234!","my_krty");//database connection


$date = date('Y-m-d');
$e_date = date('d-m-Y');

//Email Receiver//
   $to = "xxx@example.com";

   //From Header//
   $header = "From: MyKrty<undelivered@example.com>"."
";
   $header .= "MIME-Version: 1.0" . "
";
$header .= "Content-type:text/html; charset=ISO-8859-1" . "
";

   // Subject //
   $subject = "Request";


   // Message //
   $message = "<html>
    <head>
        <title></title>
        <link href='https://fonts.googleapis.com/css?family=Pacifico|Varela+Round' rel='stylesheet' />
    </head>
    <body style='font-family: ' varela=''>

        <table align='center' border='0' cellpadding='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; padding:7px; width:85%;'>
            <tbody>
                <tr>
                    <td style='text-align: center;'>
                        <strong>Tracking AWB</strong></td>
                    <td style='text-align: center;'><strong>
                        Order ID #</strong></td>

                </tr>";
$get_ndr = mysqli_query($con,"SELECT * FROM `oc_ndr_confirm` WHERE `status`='0'");
while ($row = mysqli_fetch_array($get_ndr))
{
$order_id = $row['order_id'];
$tracking_no = $row['tracking_no'];
foreach($row as $value){

$message. = "<tr><td style='text-align:center;'><strong>".$value['tracking_no']."</td><td style='text-align:center;'>".$value['order_id']."</td></tr>";
        }}

$message.= "            </tbody>
        </table>
        <p>
            &nbsp;</p>
        <p>
            &nbsp;</p>

        <p>
            We request you to kindly re-schedule the following above at your earliest.</p>

    </body>
</html> 
";    

   //Send Mail//
   $mail_send = mail($to,$subject,$message,$header);

I've tried a lot of Threads, but unable to figure out, as I don't understand arrays much. The mail function is working fine, but I'm just receiving one row only in the table, with values as 2 in both columns.

The last thread I referred to was How to Send Email with all Products in the Cart .

Though, I couldn't make it referring the above mentioned thread as it was too confusing.

  • 写回答

1条回答 默认 最新

  • dongmaomou4117 2017-03-29 10:19
    关注

    Here are 2 small edits to your code. First a small error in your html. Secondly you have a while loop already where you set your db values in your variables. No extra foreach loop is needed.

     <body style='font-family:  varela'>
    
    
    while ($row = mysqli_fetch_array($get_ndr)) {
      $order_id = $row['order_id'];
      $tracking_no = $row['tracking_no'];
      $message. = "<tr><td style='text-align:center;'><strong>".$tracking_no."</td><td style='text-align:center;'>".$order_id."</td></tr>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分