Why does this CSV export produce "undefined constants" error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Why does this CSV export produce "undefined constants" error

Post 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=/'>";
	}
	?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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']
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Got it - thanks. Heaven knows how it worked before then.... coz it did.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

True. Learnt for next time. As I think I did raise this one before as well!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply