0

I have the following code that adds data from my form to an excel spreadsheet. However, when a new user fills out the form it overwrites the data on the first row, how would I make sure that new form submissions populate a new row in this file "datacollection.xlsx"

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$title = $_POST['title'];
$company = $_POST['company'];

$fp=fopen(".datacollection.xlsx","w");
fputcsv($fp, array($name, $email, $title, $company), ';'); 
fclose($fp);

?>

Help is appreciated!

user3679330
  • 119
  • 10
  • your not wring an excel formatted file in the first pace it just a csv –  Jun 19 '17 at 04:02

1 Answers1

2

use a+ instead of w mode of fopen(), so that it places the file pointer at the end of the file. Like:

...
$fp=fopen(".datacollection.xlsx","a+"); //use a+ instead of w
fputcsv($fp, array($name, $email, $title, $company), ';');
...
Sudhir Bastakoti
  • 94,682
  • 14
  • 145
  • 149
  • Would this handle the new line appropriately? – ChickenFeet Jun 19 '17 at 03:57
  • @ChickenFeet fputcsv uses \n for line endings which might not work on some systems, so some sort of customization might be required in such case if needed. See here for reference:: https://stackoverflow.com/questions/4080456/fputcsv-and-newline-codes – Sudhir Bastakoti Jun 19 '17 at 04:07