当用户单击submit按钮时,我试图以HTML表的形式从数据库发送数据。我能收到电子邮件,但只收到了一张桌子。似乎不是数据库数据。我想用电子邮件发送一个数据库中有数据的表。这是我的密码。

<?php
require '../Mailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
//$mail->SMTPSecure = 'tls';
//$mail->SMTPAuth = true;
$mail->Host = '';
$mail->Port = ;
$mail->Username = '';
$mail->Password = '';
$mail->setFrom('');
$mail->addAddress('');
$mail->isHTML(true);
$mail->Subject = 'Student data';
$body = '<html>
        <head>
        <title></title>
         <style>
    table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
  border: 1px solid #ddd;
    text-align: left;
    padding: 8px;
    color:black
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #4CAF50;
    color: white;
}
</style>
</head>
<body>

 <?php
      $username = "root";
      $password = "";
      $host = "localhost";

      $connector = mysqli_connect("$host,$username,$password");
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysqli_select_db("school"," $connector");
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysqli_query("SELECT*FROM student ");
      ?>
       <table>
      <thead>
        <tr>
          <th>id</th>
          <th>name</th>
          <th>age</th>
          <th>class</th>
          <th>address</th>
          <th>comment</th>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = mysql_fetch_assoc( $result ) ){?>
          <tr>


              <td><?php echo $row["id"] ?></td>
              <td><?php echo $row["name"]?></td>
              <td><?php echo $row["age"] ?></td>
              <td><?php echo $row["class"] ?></td>
              <td><?php echo $row["address"] ?></td>
              <td><?php echo $row["comment"] ?></td>

            </tr>
       <?php   }
        ?>
      </tbody>
    </table>
     <?php mysql_close($connector); ?>




</body>
</html> ';

$mail->Body = $body;


//send the message, check for errors
if (!$mail->send()) {
    echo "ERROR: " . $mail->ErrorInfo;
} else {
    echo "SUCCESS";
}

最佳答案

谢谢所有回答我问题的人。这是我的系统的工作代码。

   <?php
    require '../Mailer/PHPMailerAutoload.php';

  $username = "root";
  $password = "";
  $host = "localhost";

  $connector = mysql_connect($host,$username,$password)
      or die("Unable to connect");
    echo "Connections are made successfully::";
  $selected = mysql_select_db("school", $connector)
    or die("Unable to connect");

  //execute the SQL query and return records
  $result= mysql_query("SELECT * FROM student ORDER BY id DESC limit 1");


    $mail = new PHPMailer;
    $mail->isSMTP();
    //$mail->SMTPSecure = 'tls';
    //$mail->SMTPAuth = true;
    $mail->Host = '';
    $mail->Port = ;
    $mail->Username = '';
    $mail->Password = '';
    $mail->setFrom('');
    $mail->addAddress('');
    $mail->isHTML(true);
    $mail->Subject = 'Student Data';
    $body = "<html>
    <head>
    <title>Student Data</title>
     <style>
     table {
     border-collapse: collapse;
     width: 100%;
      }

    th, td {
    border: 1px solid #ddd;
    text-align: left;
    padding: 8px;
    color:black
     }

   tr:nth-child(even){background-color: #f2f2f2}

     th {
background-color: #4CAF50;
color: white;
    }
  </style>
  </head>
  <body>

   <table>
  <thead>
    <tr>
      <th>id</th>
      <th>Name</th>
      <th>Age</th>
      <th>Class</th>
      <th>Address</th>
      <th>comment</th>
    </tr>
  </thead>
  <tbody>";

while( $row = mysql_fetch_assoc( $result ) ){

      $body.= "<tr>
      <td>".$row['id']."</td>
      <td>".$row['name']."</td>
      <td>".$row['age']."</td>
      <td>".$row['class']."</td>
      <td>".$row['address']."</td>
      <td>".$row['comment']."</td>
      </tr>";
   }

   $body.="</tbody></table></body></html>";
   ?>
    <?php mysql_close($connector); ?>

    <?php
    $mail->Body = $body;


  //send the message, check for errors
   if (!$mail->send()) {
   echo "ERROR: " . $mail->ErrorInfo;
   } else {
       echo "SUCCESS";
    }
     ?>

关于php - 如何使用HTML表格和PHP在电子邮件中发送数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34487574/

10-10 22:05
查看更多