Jump to content

Download file and update page


cowtan

Recommended Posts

I have a script that does the following -

 

fetch all the data from a mysql table

output this data as a csv file so that the user can save it and view it in excel

delete most of the data from the table

display a page showing what data is left in the table

 

The trouble is it doesn't work. It does the deletion and displays the page but doesn't download the file. If I comment out the bit to display the page, it works fine. I believe this is related to the old hoary chestnut of output buffering and sending output in the wrong order etc etc.

 

Is what I'm trying to do possible, and any suggestions on how?

 

Code is below -

 

$sessionLength = -SESSIONLENGTH;
$deleteRecords = trim(strip_tags(mysql_real_escape_string($_POST["deleteRecords"])));

$query = "SELECT * FROM logins ORDER BY loginDate DESC";
$result = mysql_query($query);
if (($result) && (mysql_num_rows($result) != 0)) {
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=\"logins.csv\"");
	$data="username, ip address, date, time \n";
	while ($r = mysql_fetch_array($result)) {
		$username = $r["loginUsername"];
		$IP = $r["loginIP"];
		$date = date("d/m/Y", strtotime($r["loginDate"]));
		$time = date("H:i:s", strtotime($r["loginDate"]));
		$data .= "$username, $IP, $date, $time \n";
	}
	echo $data;
	if ($deleteRecords) {
		$query = "DELETE FROM logins WHERE loginDateAccess < DATE_ADD(NOW(),INTERVAL $sessionLength HOUR)";
		$result = mysql_query($query);
		if ($result) {
			header("Location: loginList.php");
		}
	}
}

 

Thanks, Gordon

Link to comment
https://forums.phpfreaks.com/topic/129538-download-file-and-update-page/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.