I am having an issue with my MySQL query am not sure where the error is exactly.
Have some data in MySQL database, when I try to export some selected data as Excel format it works but it dublicates the data.
This is my database and the table name is tbl_employee
Mysql database
This is the output of the selected data on webpage
Webpage data
Below is the code although I saw this online and try to modify it by adding a checkbox to select some desire data
index.php
<?php
$connect = mysqli_connect("localhost", "root", "", "export");
$query = "SELECT * FROM tbl_employee ORDER BY id desc";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Export Mysql Table Data to CSV file in PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:900px;">
<h2 align="center">Export Mysql Table Data to CSV file in PHP</h2>
<h3 align="center">Employee Data</h3>
<br />
<form method="post" action="export.php" align="center">
<input type="submit" name="export" value="CSV Export" class="btn btn-success" />
<br />
<div class="table-responsive" id="employee_table">
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="5%">Select</th>
<th width="25%">Name</th>
<th width="35%">Address</th>
<th width="10%">Gender</th>
<th width="20%">Designation</th>
<th width="5%">Age</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Id"]; ?></td>
<td><input type="checkbox" id="vehicle1" name="txtSelectedId[]" value="<?php echo $row["Id"]; ?>"></td>
<td><?php echo $row["col_name"]; ?></td>
<td><?php echo $row["col_address"]; ?></td>
<td><?php echo $row["col_gender"]; ?></td>
<td><?php echo $row["col_designation"]; ?></td>
<td><?php echo $row["col_age"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</form>
</div>
</body>
</html>
export.php
if (isset($_POST["export"])) {
$connect = mysqli_connect("localhost", "root", "", "export");
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("php://output", "w");
fputcsv($output, array('Name', 'Address', 'Gender', 'Designation', 'Age'));
foreach ($_POST["txtSelectedId"] as $selectedbox) {
$query = "SELECT * from tbl_employee WHERE id = '$selectedbox' ORDER BY id ";
$result = mysqli_query($connect, $query);
$display = mysqli_fetch_array($result);
fputcsv($output, $display);
}
}
fclose($output);
This is what I get when I open the downloaded file in Excel
The downloaed file result
As you can see it suppose to be single data entry not double please I want the data to display in the same column
I want it to be like this
My desired result
And also I also like to export this same data with checkbox as PDF and download too.
Please anyone help
Lee Silver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1