Reputation: 43
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";
}
Upvotes: 1
Views: 4338
Reputation: 43
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";
}
?>
Upvotes: 1
Reputation: 14992
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";
}
Upvotes: 0
Reputation: 52
Above your mysql connection data ($username, ...) is still a php opening tag.
Upvotes: 0