skip to Main Content

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);
}

2

Answers


  1. You should open your file with the append flag

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

    This will let you append lines instead.

    Login or Signup to reply.
  2. You should use a+ mode.

    For more about fopen modes refer to fopen

    $fvisitas = fopen($csvfile, "a+");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search