user3679330
user3679330

Reputation: 139

Add Data to a new row in excel file using PHP

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!

Upvotes: 1

Views: 2104

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

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), ';');
...

Upvotes: 3

Related Questions