当我单击报告时,我能够获取整个表格数据,但是当我将帖子分配给变量时,它不会生成单人报告。我无法找到错误所在。
export.php
<?php
$host = "localhost"; // MySQL host name eg. localhost
$user = "root"; // MySQL user. eg. root ( if your on localserver)
$password = ""; // MySQL user password (if password is not set for your root user then keep it empty )
$database = "demo"; // MySQL Database name
$con = new mysqli($host, $user, $password, $database);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$id = $_POST['id']; // missing here
$query ="SELECT id,attendants_name,task,stat, count(*)
FROM allotted WHERE id='$id'
GROUP BY stat ";
if (!$result = mysqli_query($con, $query)) {
exit(mysqli_error($con));
}
$users = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$users[] = $row;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Users.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Task Id', 'Name','Task','Status','No. of Tasks'));
if (count($users) > 0) {
foreach ($users as $row) {
fputcsv($output, $row);
}
}
?>
report.php
echo "<a href='#' onclick='Export()' style='color:#fff;' class='btn btn-task bt1'> Report </a> ";
function Export() {
var conf = confirm("Export users to CSV?");
if(conf == true) {
window.open("export.php", '_blank');
}
}
上面提到了报告代码。我想获取具有从数据库中获取的值的单个用户的报告。我不想在查询中提供id ='1'之类的东西。我希望在搜索人的ID时动态地产生价值。
答案 0 :(得分:1)
您正在从发布请求中获取$ id,但是我看不到您正在发送发布请求。将ID附加到URL并发送到export.php并使用$ _GET获取。
请参见下面的示例
report.php
// attach id with onlick function like below (dynamically)
echo "<a href='#' onclick='Export(id)' style='color:#fff;' class='btn btn-task bt1'> Report </a> ";
function Export(id) {
var conf = confirm("Export users to CSV?");
if(conf == true) {
window.open("export.php?id=" + id, '_blank'); //attach the id with URL (GET method)
}
}
export.php
<?php
$host = "localhost"; // MySQL host name eg. localhost
$user = "root"; // MySQL user. eg. root ( if your on localserver)
$password = ""; // MySQL user password (if password is not set for your
root user then keep it empty )
$database = "demo"; // MySQL Database name
$con = new mysqli($host, $user, $password, $database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$id = $_GET['id']; // retrieve URL binded id from $_GET method
$query ="SELECT id,attendants_name,task,stat, count(*)
FROM allotted WHERE id='$id'
GROUP BY stat ";
if (!$result = mysqli_query($con, $query)) {
exit(mysqli_error($con));
}
$users = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$users[] = $row;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Users.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Task Id', 'Name','Task','Status','No. of Tasks'));
if (count($users) > 0) {
foreach ($users as $row) {
fputcsv($output, $row);
}
}
?>