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
You should open your file with the append flag
This will let you append lines instead.
You should use
a+
mode.For more about fopen modes refer to fopen