Export table to Excel in php

Hi i have a table which display records from mysql db, after displaying data’s, I’m getting error when clicking Export to Excel, below is my action code excel.php and index,php can you help me what am i missing.

<?php
$connect = mysqli_connect("localhost", "root", "", "bio_db");
$output = '';
if(isset($_POST["export_excel"]))
	{
		$sql = "SELECT * FROM daily_data2 ORDER by Userid ASC";
		$result = mysqli_query($connect, $sql);
		if(mysqli_num_rows(result) > 0)
			{
				$output .= '
					<table class="table bordered="1">
						<tr>
							<th>Userid</th>
							<th>Name</th>
							<th>Campaign</th>
							<th>Date</th>
							<th>Hoursworked</th>
							<th>Overtime</th>
						</tr>
						';
						while($row = mysqli_fetch_array($result))
						{
							$output .= '
								<tr>
									<td>'.$row["Userid"].'</td>
									<td>'.$row["Name"].'</td>
									<td>'.$row["Campaign"].'</td>
									<td>'.$row["Date"].'</td>
									<td>'.$row["Hoursworked"].'</td>
									<td>'.$row["Overtime"].'</td>
								</tr>
							';
						}
						$output .= '</table>';
						header("Content-Type: application/xls");
						header("Content-Disposition: attachment; filename=download.xls");
						echo $output;
			}		
	}
?>

Below is my index.php

 <?php

	if(isset($_POST['search']))
	{
		//$valueToSearch = $_POST['valueToSearch'];
		$cmbDept = $_POST['cmbDept'];
		$query ="SELECT * FROM daily_data2 WHERE Campaign LIKE '".$cmbDept."'";
		$search_result = filterTable($query);
	}
	else {
		$query = "SELECT * FROM daily_data2";
		$search_result = filterTable($query);
	}
	 function filterTable($query)
	{
		$connect = mysqli_connect("localhost", "root", "","bio_db");
		$filter_Result = mysqli_query($connect, $query);
		return $filter_Result;
	}				

?>
<html>
	<head>
	<title>Employee Logs</title>
	<style>
			table,tr,th,td
			{
				border: 1px solid black;
			}
	</style>

	</head>
	<body>
		<h2 align="center">Time and Attendance Monitoring</h2>

		<center>
		<form method="post" action="excel.php">
			<input type="submit" name="export_excel" class="btn btn-success" value="Export to Excel">
		</form>

		<form action="index.php" method="post">
		<select id="cmbDept" name="cmbDept">
			<option value = '' selected="selected">Filter by Department</option>
				<option value = 'TKV'>TKV</option>
				<option value = 'NA'>NA</option>
				<option value = 'PURE-INC'>PURE INC</option>
				<option value = 'DUTY-FREE'>DUTY-FREE</option>
				<option value = 'HQL'>HQL</option>
				<option value = 'PRO-XPN'>PRO-XPN</option>
				<option value = 'Mate1'>Mate1</option>
				<option value = 'STUDENT-rUS'>STUDENT-rUS</option>
				<option value = 'COLLECTIONS'>COLLECTIONS</option>
				<option value = 'NTD'>NTD</option>
				<option value = 'DATA RESEARCHER'>DATA RESEARCHER</option>
				<option value = 'VA'>DATA RESEARCHER</option>

			</select>			
			<input type="submit" name="search" value="Search"><br><br>
							
		</center>

		<table align="center" width="600" border="1" cellpadding="1" cellspacing="1">
			<tr>
				<th>Userid</th>
				<th>Name</th>
				<th>Campaign</th>
				<th>Date</th>
				<th>Hoursworked</th>
				<th>Overtime</th>
			</tr>

		<?php while($row = mysqli_fetch_array($search_result)):?>
			<tr>
				<td style="text-align:center;"><?php echo $row['Userid'];?></td>
				<td width="200"><?php echo $row['Name'];?></td>
				<td style="text-align:center;"><?php echo $row['Campaign'];?></td>
				<td width="100" style="text-align:center;"><?php echo $row['Date'];?></td>
				<td style="text-align:center;"><?php echo $row['Hoursworked'];?></td>
				<td style="text-align:center;"><?php echo $row['Overtime'];?></td>
			</tr>
		
		<?php endwhile;?>
						
			</table>
		</form>	
	</body>
</html>
1 Like

so… which? what did you understand by reading it? what did you try to fix it?

1 Like

i already eliminate the error around if(mysqli_num_rows($result) > 0), i forgot to put $ to result, but unfortunately it does get the data inside my table…

You need to work on the way you present your problems to the public.

If you need just some comfort and compassion, then the current form is OK, but it’s rather off topic on a forum like this. And if you need a more technical answer, you have to provide more technical details as well. Refer to this article in order to improve, Writing the perfect question

I think what people are trying to say is “what happens?” You mention you get an error, but don’t tell us what it is. Does it not create the download link? Does it create it, but the format isn’t correct? Given that we can’t run your code, you need to say exactly what isn’t happening that should.

I can’t imagine these mismatched quotes help:

<table class="table bordered="1">
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.