Page 1 of 1

databse dump excel php [solved]

Posted: Tue Apr 19, 2011 7:31 pm
by SpiritWebb
I have a form that asks for a month and year. Then it is supposed to dump the database to an excel sheet. However, it says undefined index "csv_output" on line 37 (which is marked // ). It worked before until I changed computers and now it will not work. Can anyone help me out to get this fixed. I need to get this fixed by tomorrow night (Wednesday night).

Code: Select all

<title>ODCMM - Conway ODC</title>
<style  type="text/css" media="screen">
ul  li{
  list-style-type:none;
}
</style>
<fieldset><center><h2><strong>Monthly Report</strong></h2></center></fieldset>
<Center><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p>You must search using the month and year (ie. Month: 8 Year: 2010)</p>

<form method="post" action="monthlyreport.php?go" id="searchform">
      Month:<input type="text" name="month"> Year:<input type"text" name="year">
      <input type="submit" name="submit" value="Search">
</form></center>

<?php
// Edit the $Host, $User $Password, $DBName and $TableName vars only! //
$Host = "**************";
$User = "*****";
$Password = "******";
$DBName = "metrics";

  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['month'])){
  $month=$_POST['month'];
  $year=$_POST['year'];

$link = mysql_connect ($Host, $User, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DBName) or die('Could not select database' . mysql_error());

$select = "SELECT id, datemonth, dateday, dateyear, userid, shift, rotname, rotationcomplete, totaltapesreq, totalentered, totalremoved, comments FROM shiftmetrics WHERE datemonth LIKE '%" . $month .  "%' AND dateyear LIKE '%" . $year .  "%'";
$export = mysql_query($select); 
$fields = mysql_num_fields($export); 

for ($i = 0; $i < $fields; $i++) {
	$csv_output .= mysql_field_name($export, $i) . "\t";  //the error occurs here
}

while($row = mysql_fetch_row($export)) {
	$line = '';
	foreach($row as $value) {
		if ((!isset($value)) OR ($value == "")) {
			$value = "\t"; 
		} else {
			$value = str_replace('"', '""', $value);
			$value = '"' . $value . '"' . "\t"; 
		}
		$line .= $value;
	}
	$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=database_dump.xls");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output."\n".$data;
exit;
}
  }
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Download MySQL Table Code</title>
</head>
<body>

</body>
</html>


Re: databse dump excel php

Posted: Wed Apr 20, 2011 1:19 am
by UnknownUser
The undefined index notice indicates that you are referring to an array element that does not exist,

Re: databse dump excel php {solved}

Posted: Wed Apr 20, 2011 11:21 pm
by SpiritWebb
I ended up re-writing the file and it works now!