user783710
user783710

Reputation:

Save html form as CSV possible?

I was wondering if it's possible to save the contents of a html form into csv format?

I was hoping to use it as an alternative to using a database.

Upvotes: 0

Views: 797

Answers (5)

Karoly Horvath
Karoly Horvath

Reputation: 96258

store $_POST with fputcsv

see: http://php.net/manual/en/function.fputcsv.php

Upvotes: 0

flori
flori

Reputation: 15837

Of course. Take your fields and just put them into a file:

$data = '"'.addslashes($first).'","'.addslashes($second).'"'."\n";
file_put_contents('form.csv', $data, FILE_APPEND);

Upvotes: 0

David
David

Reputation: 218808

PHP has some built-in functionality to help with this.

as an alternative to using a database

Keep in mind that this isn't a drop-in replacement for an actual database engine. A few things to consider:

  1. How are you going to handle multiple concurrent users? Lock the file and some users just get timeouts? Race conditions can become a bit of a problem in this setup.
  2. How are you going to query the data? If the data is expected to grow, expect also the performance to drop. You can't really optimize a CSV for queries.
  3. Are you going to have multiple tables across multiple CSV files? How are you going to keep the relational integrity of the data? The application should be responsible for the integrity of the data in motion, but the data store should be responsible for the integrity of the data at rest. A CSV has no mechanism for maintaining data integrity.

Upvotes: 2

Shaun Hare
Shaun Hare

Reputation: 3871

Sure

Matter of fact it is very easy and could be achieved with one line (Although I would not reccomend during this in production always validate input and you would also need to append to the file)

file_put_contents("mycsvfile.csv",implode(",", $_POST))

Upvotes: 0

Flash
Flash

Reputation: 16703

Definitely possible. Use $_POST['var'] to get the form variables, and then fwrite them to a file (with comma delimiters or however you like). Also make sure to validate your input.

Upvotes: 0

Related Questions