I have this code below for export mysql database to excel..however, if i run this it will only export the html code of the page not the sql query..please help..thank you.
<html>
<head><title>Exports</title>
<link rel="stylesheet" href="css/css.css" />
<h2>EXPORT DATE/YEAR</h2></head>
<form action="" method="post">
<table>
<tr>
<td><input size="40" name="searchString" type="text"class="ed" />
<select name="by" class="ed">
<option>Date</option>
<option>Year</option>
</select>
<input type="submit" name="export" value="Export" id="button2" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['export']))
{
//sanitize the input
function clean($str)
{
$str = @trim($str);
if(get_magic_quotes_gpc())
{
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$searchString = strtoupper(clean($_POST['searchString']));
$by=clean($_POST['by']);
if ($by=='Date')
{
$host="localhost";
$uname="root";
$pass="Unknown";
$database = "dcde";
$connection=mysql_connect($host,$uname,$pass);
echo mysql_error();
//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or
die("Database could not be selected");
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
// Fetch Record from Database
$output = "";
$table = "po"; // Enter Your Table Name
$sql = "Select poNo,date,supplier,tAmount,code from $table WHERE date like '%$searchString%'";
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename = "myfile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}
}
?>