不久前,我编写了一些针对MySQL数据库运行查询的代码,然后遍历结果,将它们放入表格中,然后通过电子邮件发送结果。
最近,我收到了早上的邮件,有一段时间我有一个随机的空行,里面有一个“0”,如下所示:
当我运行查询时,其中没有任何0或空行。数据总是回来干净。所以,我认为这必须以某种方式在我的循环中。我不知道。无论如何......下面是我的if语句:
if ($result_count = $conn->query($sql_count)) {
while($row = $result_count->fetch_assoc()) {
$message .= '<tr style="white-space: normal;line-height: normal;font-weight: normal;font-variant: normal;font-style: normal;text-align: start;">
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["number"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["u_name"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["code"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["acode"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["name"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["success"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'
.$row["comment"].
'</td>
</tr>';
}
} else {
echo "No work orders have been entered into the system today.";
}
有人有什么想法吗?如果您需要查看其他任何内容,请告知我们。希望有人能发现我似乎看不到的东西。
修改
我会在这里添加更多内容并希望它能够清除它。所以,今天我又得到了另一份报告,并且还有更多的零行。现在几乎每天都在发生......
如果我运行查询,这是输出:
以下是表格的构建方式:
最后......这是用于生成电子邮件的页面(减去所有CSS)
<!DOCTYPE html>
<html>
<head>
<title>Daily Report</title>
<meta charset=utf-8>
</head>
<body>
<?php
require 'db/spoc_config.php';
?>
<?php
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_count = "
select number, name, u_name, code, acode, success, comment from forms where Day(date) = Day(CURDATE()) and Month(date) = Month(CURDATE()) and year(date) = YEAR(CURDATE());";
$result_count = $conn->query($sql_count);
$count = mysqli_fetch_row($result_count);
$str_count = implode($count);
date_default_timezone_set('UTC');
$today = date('l, jS F Y');
$week = date('W');
$to = "someemail@email.com";
$subject = "WOR: Daily Report " . $today;
$heading = "<b>Work Order Daily Report: </b>";
$message = "<h2>" . $heading . " " . $today . "</h2>";
$message .= '<table>';
$message .= '
<thead>
<tr>
<th>WO#</th>
<th>Engineer Name</th>
<th>Site</th>
<th>Airline</th>
<th>WO Description</th>
<th>Status</th>
<th>Comment</th>
</tr>
<tr></tr>
</thead>
<tbody>
';
if ($result_count = $conn->query($sql_count)) {
while($row = $result_count->fetch_assoc()) {
$message .= '<tr>
<td>'.$row["number"].'</td>
<td>'.$row["u_name"].'</td>
<td>'.$row["code"].'</td>
<td>'.$row["acode"].'</td>
<td>'.$row["name"].'</td>
<td>'.$row["success"].'</td>
<td>'
.$row["comment"].
'</td>
</tr>';
}
} else {
echo "No work orders have been entered into the system today.";
}
$message .= '</tbody></table>';
$header = "From:someemail@email.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$retval = mail ($to,$subject,$message,$header,'-fsomeemail@email.com');
$conn->close();
?>
好的......所以我确定数据中有零。所以,显示的是正确的....但我无法弄清楚是什么导致了零。我已经包含在表单页面下面,因为我认为它必须是如何生成的。希望有人能看到问题:
<?php
require 'db/spoc_config.php';
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect due to: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use: ' . DB_NAME . ': ' . mysql_error());
}
$u_name = mysql_real_escape_string($_POST['u_name']);
$name = mysql_real_escape_string($_POST['name']);
$code = mysql_real_escape_string($_POST['code']);
$acode = mysql_real_escape_string($_POST['acode']);
$source = mysql_real_escape_string($_POST['source']);
$number = mysql_real_escape_string($_POST['number']);
$assess = mysql_real_escape_string($_POST['assess']);
$asse = mysql_real_escape_string($_POST['asse']);
$success = mysql_real_escape_string($_POST['success']);
$status = implode ("|" , $_POST['status']);
$comment = mysql_real_escape_string($_POST['comment']);
$nchangenum = mysql_real_escape_string($_POST['nchangenum']);
$pchangenum = mysql_real_escape_string($_POST['pchangenum']);
$date = date("Y-m-d");
$sql = "INSERT INTO forms (u_name, name, code, acode, source, number, assess, asse, success, status, comment, nchangenum, pchangenum, date) VALUES ('$u_name', '$name', '$code', '$acode', '$source', '$number', '$assess', '$asse', '$success', '$status', '$comment', '$nchangenum', '$pchangenum', '$date')";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
<html>
<?php require 'head.php' ?>
<body>
<div>
<div>
<div></div>
<h3>
Airport Operations Workorder Repository
</h3>
</div>
<?php include 'nav.php';?>
<div>
<div>
<div>
</div>
<div>
<?php
session_start();
$username = $_SESSION['username'];
if($_SESSION['username']){
echo "<div><p class='bold_user'>Welcome, " . $_SESSION['username'] . ".</p><a href='logout.php'>(logout)</a></div>";
}
else die("<div class='reg'>You must be logged in.</div>");
?>
<div class="container">
<div class="outer">
<div class="inner">
<div class="centered">
<?php echo "<div class='loginerr'>You have successfully entered Work Order # " . $number . ". Please return to <a href='page.php'>the previous page</a> to enter more.</div>" ?>
</div>
<div>
</div>
<div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
根据我的理解,请尝试以下步骤: -
答案 1 :(得分:0)
您可以将$ message包装在验证if():
中if($row["number"] && $row["number"] > 0)
{
$message .= '<tr style="white-space: normal;line-height: normal;font-weight: normal;font-variant: normal;font-style: normal;text-align: start;">
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["number"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["u_name"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["code"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["acode"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["name"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["success"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'
.$row["comment"].
'</td>
</tr>';
}
else
{
// skip this line - do nothing
}