我是新手,在PHP和MySQL。我知道其他语言,但对于这个项目,它是用PHP完成的。我在网上找到了一些我正在调整的代码,这样它就可以完成我的任务。我正在尝试从MySQL数据库导出数据,将其显示在屏幕上,并提供下载到excel的选项。代码运行并正常工作,但是它没有将SQL数据库中的数据放在行上,而只是放在表列上。
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'hcap');
$data = mysqli_query($conn,"SELECT `Org_ID`,'Org_Name', 'Org_Address', 'Org_Address2', 'Org_City', 'Org_State', 'Org_Zip', 'Org_County',
'Org_Website', 'Org_Phone', 'Org_fax', 'Org_Email'
FROM `organization`");
if(isset($_POST["ExportType"])) {
switch($_POST["ExportType"]) {
case "export-to-excel" :
// Submission from
$filename = $_POST["ExportType"] . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
ExportFile($data);
//$_POST["ExportType"] = '';
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}
}
function ExportFile($records) {
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;
}
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<title>HCAP Organization Report</title>
<div><h3>HCAP Organization Report</h1></div>
<div>
<div id="container" >
<div class="col-sm-6 pull-left">
<div class="well well-sm col-sm-12">
<b id='project-capacity-count-lable'><?php echo count($data);?></b> records found.
<div class="btn-group pull-right">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu" id="export-menu">
<li id="export-to-excel"><a href="#">Export to excel</a></li>
<li class="divider"></li>
<li><a href="#">Other</a></li>
</ul>
</div>
</div>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
<input type="hidden" value='' id='hidden-type' name='ExportType'/>
</form>
<table id="" class="table table-striped table-bordered">
<tr>
<th>Organization ID</th>
<th>Organization Name</th>
<th>Organization Address</th>
<th>Organization Address 2</th>
<th>City </th>
<th>State </th>
<th>Zip Code </th>
<th>County </th>
<th>Website </th>
<th>Phone Number </th>
<th>Fax Number </th>
<th>E-Mail Address </th>
</tr>
<tbody>
<?php foreach($data as $row):?>
<tr>
<td><?php echo $row ['Org_ID']?></td>
<td><?php echo $row ['Org_Name']?></td>
<td><?php echo $row ['Org_Address']?></td>
<td><?php echo $row ['Org_Address2']?></td>
<td><?php echo $row ['Org_City']?></td>
<td><?php echo $row ['Org_State']?></td>
<td><?php echo $row ['Org_Zip']?></td>
<td><?php echo $row ['Org_County']?></td>
<td><?php echo $row ['Org_Website']?></td>
<td><?php echo $row ['Org_Phone']?></td>
<td><?php echo $row ['Org_fax']?></td>
<td><?php echo $row ['Org_Email']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
jQuery('#export-menu li').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
case 'export-to-excel' :
$('#hidden-type').val(target);
//alert($('#hidden-type').val());
$('#export-form').submit();
$('#hidden-type').val('');
break
}
});
});
</script>
发布于 2018-11-06 13:30:27
您可以使用单引号而不是反引号。这就是为什么您会看到文本列名,您希望在其中看到数据库值。
改用下面的代码:
SELECT `Org_ID`,`Org_Name`, `Org_Address`, `Org_Address2`, `Org_City`,
`Org_State`, `Org_Zip`, `Org_County`, `Org_Website`, `Org_Phone`,
`Org_fax`, `Org_Email`
FROM `organization`
或者不使用任何引号(因为这对于您的查询是不必要的--没有保留关键字或特殊字符需要担心)。
我在我的本地主机上测试了这个建议的正确性。
https://stackoverflow.com/questions/53165987
复制相似问题