Page 1 of 1

Why does this CSV export produce "undefined constants" error

Posted: Wed May 25, 2016 6:38 am
by simonmlewis
We use this for a client to export a list of users and their samples status.
But it's producing a ton of these:

[text]Notice: Use of undefined constant firstname - assumed 'firstname' in C:\xampp\phpMyAdmin\site\csv_NEWrequestssent.php on line 19[/text]

Code: Select all

<?php
session_start();
		if (isset($_SESSION["loggedin"])) 
            {
            $usertype = $_SESSION["usertype"];
$id = isset($_GET['id']) ? $_GET['id'] : null;
if ($usertype == "admin" || $usertype == "moderator") {
include "dbconn.php";

$csv_output = '"Firstname","Lastname","House","Street","Town","County","Postcode","Product","Date Requested","Date Sent"';
$csv_output .= "\015\012";

$result = mysql_query("SELECT * FROM samples WHERE dateposted IS NOT NULL ORDER BY dateposted");
while($row = mysql_fetch_array($result)) 
{   
  $resultu = mysql_query("SELECT * FROM admin WHERE id = '$row[userid]'");
  while($rowu = mysql_fetch_array($resultu)) 
  {  
  $csv_output .= '"'.$rowu[firstname].'","'.$rowu[lastname].'","'.$rowu[address1].'","'.$rowu[address2].'","'.$rowu[town].'","'.$rowu[county].'","'.$rowu[postcode].'","'.$row[prodname].'","'.$row[dateadded].'","'.$row[dateposted].'"';
    $csv_output .= "\015\012";
  }
}
  //You cannot have the breaks in the same feed as the content. 
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv; filename=RequestsSent_" . date("Ymd") .".csv");
  print $csv_output;
  exit;
       	mysql_close($sqlconn);
  echo "Extract in progress - close page when completed.";
	}}

	else 
	{
	echo "<meta http-equiv='Refresh' content='0 ;URL=/'>";
	}
	?>

Re: Why does this CSV export produce "undefined constants" e

Posted: Wed May 25, 2016 7:31 am
by Celauran
Because you have undefined constants. Array keys need to be quoted when they're strings.

Code: Select all

$rowu[firstname]
should be

Code: Select all

$rowu['firstname']

Re: Why does this CSV export produce "undefined constants" e

Posted: Wed May 25, 2016 8:32 am
by simonmlewis
Got it - thanks. Heaven knows how it worked before then.... coz it did.

Re: Why does this CSV export produce "undefined constants" e

Posted: Wed May 25, 2016 8:39 am
by Celauran
It's bad practice, but should work regardless. PHP is able to figure out what you meant. Maybe a difference in PHP versions and/or error reporting levels? Either way, better to fix it.

Re: Why does this CSV export produce "undefined constants" e

Posted: Wed May 25, 2016 8:44 am
by simonmlewis
True. Learnt for next time. As I think I did raise this one before as well!