Home > Back-end >  CSV from a form php
CSV from a form php

Time:11-11

I'm a begginer in PHP and I want to get data from a form which contains a name,a comment and a name of a photo the user selects, and put it in a csv.But when i try to write the date, the data i already have in the csv is overwrited.So every time I only have one line in my csv with the newest data.

I want data to be introduced in a new csv line everytime the form is used like this:

Russel,Hello,Football.jpg
James,Bye,Coke.png

Instead of being overwrited like this:

James,Bye,Coke,png

This is what i tried:

if (isset($_POST['submit'])) {
    $name = $_POST["nom"];
    $comment = $_POST['com'];
    $image = $_FILES['imag']['name'];
    $csvfile = 'fichero.csv';
    $fvisitas = fopen($csvfile, "c ");
    $lista = array(
       array($name, $comment, $image)
    );
    foreach ($lista as $campos) {
        fputcsv($fvisitas, $campos);
    }
    fclose($fvisitas);
}

CodePudding user response:

You should open your file with the append flag

  $fvisitas = fopen($csvfile, "a");

This will let you append lines instead.

CodePudding user response:

You should use a mode.

For more about fopen modes refer to fopen

$fvisitas = fopen($csvfile, "a ");
  • Related