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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?