1

Im trying to add data to a csv file, however every time i run the php script it will add the data two or three times and that is of course super annoying! Just wondering if anyone has an idea of why this is happening? PHP Code:

$my_file2 = 'TEMP/temp.csv';
$handle = fopen($my_file2, 'a');
$data = "Mobile,".$mobi.",Message,".$message."\n";
fwrite($handle, $data);
fclose($my_file2);

An example of the csv file with double entry: enter image description here

2
  • is this code your entire PHP script ? is it in a loop ? Commented Jul 17, 2019 at 10:43
  • 4
    You have added mode fopen mode as a so it will append data every time, not overwrite. php.net/manual/en/function.fopen.php Commented Jul 17, 2019 at 10:43

1 Answer 1

-1

Have you tried to use fputcsv function?

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\\" ]]] ) : int
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain that further? Why should one try that and how? Keep in mind that explaining your answer helps others to understand it
Because this is the fist function i've found when i've searched how to save csv in files with php. I've linked also the page to php manual.
Linking to external sources does not help to solve a problem. Please have a look at stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.