1

I'm trying to send data from database in the form of a HTML table when user click submit button. I'm able to receive the email but only table received. It doesn't seem database data. I want to email a table with data in database. Here is my code.

<?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";
}

1
  • print the email before sending and check is it correct ,i think its a php query issue Commented Dec 28, 2015 at 4:22

3 Answers 3

1

Thanks for everyone, who answer to my question. Here is the working code for my system.

   <?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";
    }
     ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Above your mysql connection data ($username, ...) is still a php opening tag.

Comments

0

Your code is all wrong, you're opening php tags inside a php tag.

Also you were using mysqli_ the wrong way, then trying to close with mysql_ .

Here, I fixed it for you:

<?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>
';
      $username = "root";
      $password = "";
      $host = "localhost";

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

      //execute the SQL query and return records
      $result = mysqli_query($connector, "SELECT*FROM student ");
      $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 = mysqli_fetch_assoc($connector, $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> ';
$mail->Body = $body;
//send the message, check for errors
if (!$mail->send()) {
    echo "ERROR: " . $mail->ErrorInfo;
} else {
    echo "SUCCESS";
}

2 Comments

I have tried above code, but I'm getting syntax error. <?php // syntax error in this line $username = "root"; $password = ""; $host = "localhost"; when I deleted this php line the syntax error goes to next line.
Somebody has fixed it for me, I didn't notice the opening PHP tag, try it now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.