gpt4 book ai didi

php - 从 php 表中导出一行

转载 作者:行者123 更新时间:2023-11-29 16:37:41 25 4
gpt4 key购买 nike

我尝试将单行导出为 CSV,但我的代码却获取所有行。

这是我的屏幕截图:export 这是我的代码:索引.php

<?php
db();
global $link;

$query = "SELECT id, name, address, contact, age, birthday FROM info";
$result = mysqli_query($link, $query);

while($row = mysqli_fetch_assoc($result)){
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$contact = $row['contact'];
$age = $row['age'];
$birthday = $row['birthday'];
?>

<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['birthday']; ?></td>
<td><a href="read.php?id=<?php echo $id?>">View</a></td>
<td><a href="update.php?id=<?php echo $id?>">Update</a></td>
<td><a href="delete.php?id=<?php echo $id?>" onClick="return confirm('Delete This account?')">Delete</a></td>
<td>
<form method="post" action="export_excel.php" id="export_sql">
<input type="submit" name="export" value="EXCEL">
</form>
</td>
<td>
<form method="post" action="export_pdf.php" id="export_pdf" target="_blank">
<input type="submit" name="export_pdf" value="PDF">
</form>
</td>
</tr>

<?php } ?>

export_sql.php

<?php
if (isset($_POST["export"])) {
$link = mysqli_connect("localhost", "root", "", "bondad-crud") ;
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('id','name','address','contact','age','birthday', 'date'));
$query = "SELECT * FROM info WHERE id='$id'";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
fputcsv($output, $row);
}
fclose($output);
}


?>

我尝试通过 id 获取行,但仍然出现错误。

最佳答案

更新答案

主要问题是您为 while 循环的每次迭代添加一个新表单。这将导致您遇到的问题类型。我已经删除了<form>标签从循环内部到循环外部。

您的第二个问题是您的 ID 未正确传递到导出页面。

我更改了您的两个提交按钮的名称,以便它们将由您的帖子数据中的数组表示。数组中第一个元素的键是 id。

db();
global $link;

$query = "SELECT id, name, address, contact, age, birthday FROM info";
$result = mysqli_query($link, $query);
echo
'<form method="post" action="export_excel.php" id="export_sql">';

echo
'<table>';

while($row = mysqli_fetch_assoc($result)){

$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$contact = $row['contact'];
$age = $row['age'];
$birthday = $row['birthday'];

echo
'<tr>' .
'<td>' . $row['id'] . '</td>' .
'<td>' . $row['name'] . '</td>' .
'<td>' . $row['address'] . '</td>' .
'<td>' . $row['contact'] . '</td>' .
'<td>' . $row['age'] . '</td>' .
'<td>' . $row['birthday'] . '</td>' .
'<td><a href="read.php?id=<' . $id . '">View</a></td>' .
'<td><a href="update.php?id=<' . $id . '">Update</a></td>' .
'<td><a href="delete.php?id=<' . $id . '" onClick="return confirm("Delete This account?")">Delete</a></td>' .
'<td><input type="submit" name="export[' . $id . ']" value="EXCEL">' . '</td>' .
'<td><input type="submit" name="export_pdf[' . $id . ']" value="PDF"></td>' .
'</tr>';

}

echo
'</table>';

echo
'</form>';

请注意,您现在只有一张表单。

为了您的export_sql.php page :

我们将获取 $_POST[export] 的第一个键数组或 $_POST[export_pdf]这就是您的 ID。现在我们有一个 ID 可以传递给正确的查询。

请注意,您现在可以从此页面下载 Excel 和 PDF。

<?php
//Do code for Excels
if (isset($_POST["export"])) {
$link = mysqli_connect("localhost", "root", "", "bondad-crud") ;
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('id','name','address','contact','age','birthday', 'date'));

reset($_POST["export"]); //Added this.
$id = key($_POST["export"]); //And this.

$query = "SELECT * FROM info WHERE id='$id'";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
fputcsv($output, $row);
}
fclose($output);
}

//Handle the PDF's.
if (isset($_POST["export_pdf"])) {

reset($_POST["export_pdf"]); //Added this.
$id = key($_POST["export_pdf"]); //And this.

//.....Do the rest of code for the PDF's

}

?>

希望有帮助。

关于php - 从 php 表中导出一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com