1

I'm trying to write data to a csv file, through the below code.

Html

<html>
<body>
  <form action="writeToExcel.php" method="post">
    <center><h1>Personal Learning Goals Form</h1></center>
    Student ID: &nbsp;&nbsp;&nbsp;<input type="text" name="StudID" /><br/>
    Student Name: &nbsp;&nbsp; <input type="text" name="StudName" /><br/> 
    Comment 1: &nbsp;&nbsp;<input type="text" name="Com1" /><br/>
    Comment 2: &nbsp;&nbsp;<input type="text" name="Com2" /><br/> 
    Comment 3: &nbsp;&nbsp;<input type="text" name="Com3" /><br/>
    Comment 4: &nbsp;&nbsp;<input type="text" name="Com4" /><br/> 
   <input type="submit" value="Submit Form">
  </form>
</body>
</html>

I'm using kevin's class to write data csv.php

here's my php

<?php

require_once 'CSV.php';
$out_file = 'learning_goals.csv';

$csv = new CSV();

$headers = array ( // one row
  array (          // six columns
    'StudID',
    'StudName',
    'Com1',
    'Com2',
    'Com3',
    'Com4'
  )
);

if ( !is_file( $out_file ) ) {
  $csv->write_table( $headers, $out_file );
}

$data = array ( // one row
  array (       // six columns
    $_POST['StudID'],
    $_POST['StudName'],
    $_POST['Com1'],
    $_POST['Com2'],
    $_POST['Com3'],
    $_POST['Com4']
  )
);

$csv->append_table( $data );

// now you have a csv file in the same directory as this script
// you can read and edit it with excel, or also this class
// ... for demonstration purposes ...
// comment out the following for production

$table = $csv->parse_table();
print_r ( $table );

?>

I'm new to csv, I have just created a csv file named learning_goals.csv and created 5 inputs "StudID", "StudName", "Com1", "Com2", "Com3", "Com4"

When I input my data and push it, it shows

Notice: Undefined property: CSV::$csv_file in /opt/lampp/htdocs/data-export/CSV.php on line 178 UNABLE TO APPEND TO CSV FILE

Does anyone know how I can solve this?

Matt
  • 74,352
  • 26
  • 153
  • 180
Ezhil
  • 389
  • 1
  • 4
  • 18

1 Answers1

0

You're trying to append to a file without specifying the filename in either the constructor or the load_file(filename) function.

Consider using write_table(data [,filename]) instead, or use the load functions if you really want to be appending.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105