我正在尝试使用php将保存在mysql数据库中的数据下载到word文档中。
我现在拥有的代码的工作原理如下,显示数据库学生表中的所有数据单击下载按钮,将这些数据下载到Microsoft word文档中。
然而,它只是在学生表的记录上打印出来,当你试图打开文档时,它会说内容已损坏(当使用谷歌chrome时,它在firefox中打开得很好)。
任何帮助都将不胜感激。(参见下面的代码)
<?php
include("dbConnect.php");
$dbQuery = $db->prepare("select * from student");
$dbQuery->execute();
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$Forename = $dbRow['Forename'];
$Surname = $dbRow['Surname'];
$Email = $dbRow['Email'];
$B_number = $dbRow['B_number'];
$School = $dbRow['School'];
$Campus = $dbRow['Campus'];
$Research_Institute = $dbRow['Research_Institute'];
$FTPT = $dbRow['FTPT'];
}
$doc_body = "<h1>Students</h1>";
?>
<head>
<title>Students</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">
</head>
<form name="export_form" action="<?php
echo ($_SERVER['PHP_SELF']);
?>" method="post">
<input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
</form>
<?php
if (isset($_POST['submit_docs'])) {
header("Content-Type:application/msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=test.docx");
}
echo "<html>";
echo "$doc_body";
echo "<table class=table table-striped table id=student>
<tr>
<th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
</tr>
<tr><td>$ID</td><td>$Forename</td><td>$Surname</td> <td>$Email</td><td>$B_number</td><td>$School</td><td>$Campus</td><td>$Research_Institute</td><td>$FTPT</td>
</tr>;
</table>";
echo "</html>";
?>
while循环中的代码会覆盖以前的值,因此您最终会得到$ID
、$Forename
、…中最后一个条目的值。您需要在while循环中生成输出。此外,您必须确保在生成单词内容时不会发送网站内容。
试试这样的结构:
<?php
include ("dbConnect.php");
function databaseOutput() {
$dbQuery = $db->prepare("select * from student");
$dbQuery->execute();
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $dbRow['ID']; ?></td>
<td><?php echo $dbRow['Forename']; ?></td>
<td><?php echo $dbRow['Surname']; ?></td>
<td><?php echo $dbRow['Email']; ?></td>
<td><?php echo $dbRow['B_number']; ?></td>
<td><?php echo $dbRow['School']; ?></td>
<td><?php echo $dbRow['Campus']; ?></td>
<td><?php echo $dbRow['Research_Institute']; ?></td>
<td><?php echo $dbRow['FTPT']; ?></td>
</tr>
<?php
}
} // end of function databaseOutput()
if ($_POST['submit_docs']) { // word output
header("Content-Type:application/msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=test.docx");
?>
<html>
<body>
<h1>Students</h1>
<table>
<tr>
<th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
<?php
exit; // end of word output
}
?>
<html>
<head>
<title>Students</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/mycss.css">
</head>
<body>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
<input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" /> <a href="https://dunluce.infc.ulst.ac.uk/cw11ba/project/Project/admin.php"><button type="button" class= "btn btn-block">Go back to Admin Area</button></a>
</form>
<table class="table table-striped" id="student">
<tr>
<th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>
</tr>
<?php databaseOutput(); ?>
</table>
</body>
</html>
我没有测试这个,但类似的东西应该可以工作。
我会将标题更改为:(未测试,但应该可以工作)
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=test.doc");
也可以在<html>
标签和$doc_body
:之间添加此行
<meta http-equiv='"Content-Type'" content='"text/html; charset=Windows-1252'">
这将为您提供一个与更多浏览器兼容的旧版本,如果需要,它只需更新文件。希望这能有所帮助!
修改了完整代码:
<?php
if(isset($_POST['submit_docs']))
{
header("Content-type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment;Filename=test.doc");
}
$pri = "<html>";
$pri .= "<meta http-equiv='"Content-Type'" content='"text/html; charset=Windows-1252'">";
$pri .= $doc_body;
$pri .= "<table class=table table-striped table id=student>";
$pri .= "<tr>";
$pri .= "<th>ID</th><th>Forename</th><th>Surname</th><th>Email</th><th>B Number</th><th>School</th><th>Campus</th><th>Research Institute</th><th>FT/PT</th>";
$pri .= "</tr>";
$pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";
$pri .= "</tr>";
$pri .= "</table>";
$pri .= "</html>";
echo $pri;
?>
通过这种方式,您可以转义所有空白,并输出php变量,而不会冒它们被解释为广告文本的风险。
循环查看结果并将其全部输出到WORD文件中:删除文档顶部的while循环并替换此行:
$pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";
这个:
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC))
{
$ID = $dbRow['ID'];
$Forename = $dbRow['Forename'];
$Surname = $dbRow['Surname'];
$Email = $dbRow['Email'];
$B_number = $dbRow['B_number'];
$School = $dbRow['School'];
$Campus = $dbRow['Campus'];
$Research_Institute = $dbRow['Research_Institute'];
$FTPT = $dbRow['FTPT'];
$pri .= "<tr><td>".$ID."</td><td>".$Forename."</td><td>".$Surname."</td> <td>".$Email."</td><td>".$B_number."</td><td>".$School."</td><td>".$Campus."</td><td>".$Research_Institute."</td><td>".$FTPT."</td>";
}
应该像符咒一样工作:)