1

I'm using below code to get data from PHP form. Its working fine.

	<?php

$email=$_POST['email'];
$name=$_POST['name'];
$designation=$_POST['designation'];
$company=$_POST['company'];
$phone=$_POST['phone'];
$data=$name.",".$email.",".$designation.",".$company.",".$phone;

$file="file.csv";


file_put_contents($file, $data . PHP_EOL, FILE_APPEND);


?>

Whenever any user puts information in the form with separated (.) then name and email come in different cells in .csv file.

below is issue/csv image enter image description here

How it will solved?

Thanks!

1
  • just build an array of the values, then use fputcsv(); PHP's built-in csv writer function, that knows how to handle a separator in the values Commented Jan 9, 2018 at 16:25

1 Answer 1

2

Use PHP's fputcsv. Let it handle the particulars of CSV formatting for you!

<?php

$email       = $_POST['email'];
$name        = $_POST['name'];
$designation = $_POST['designation'];
$company     = $_POST['company'];
$phone       = $_POST['phone'];

$data = [
         $name,
         $email,
         $designation,
         $company,
         $phone,
];

$file = fopen('file.csv', 'a'); // Append data to end of file.

fputcsv($file, $data);

If you need a delimiter other than the default (,) put that for the third argument. Same for enclosure & escape chars (", \) as 4th and 5th args.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Cameron Hurd

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.