将html输入写入csv文件

I try to write from html form to csv file using php. I have a problem that nothing is wrote in the file .

I want to be like this:

a,b,c,d

and if I have an empty input, it will be like this

a,,b,c,d

This is the code, what's wrong on it?

$csv=array();
$number=$_POST['txt_number'];
$description=$_POST['txt_description'];
$division=$_POST['txt_division'];
$stage=$_POST['txt_stage'];
$category=$_POST['txt_category'];
$priority=$_POST['txt_priority'];
$frequency=$_POST['txt_frequency'];
$notapprove=$_POST['txt_notapprove'];
$approve=$_POST['txt_approve'];
$notexist=$_POST['txt_notexist'];
$wo=$_POST['txt_wo'];
$duration=$_POST['duration'];
$startdate=$_POST['startdate'];
$enddate=$_POST['enddate'];
$asd=$_POST['txt_asd'];
$add=$_POST['txt_add'];
$aduration=$_POST['txt_aduration'];
$transferredto=$_POST['txt_transferredto'];
$prb=$_POST['txt_percentage'];
$note=$_POST['txt_note'];
$projectname=$_POST['txt_projectname'];
if($exist=="Not Approve"){$a="Not Approve";}
if($exist=="Approve"){$b="Approve";}
if($exist=="Not Exist"){$c="Not Exist";}
$csv[]=$number;
$csv[]=$description;     
$csv[]=$division;
$csv[]=$stage;
$csv[]=$category;
$csv[]=$priority;
$csv[]=$frequency;
$csv[]=$notapprove;
$csv[]=$approve;
$csv[]=$notexist;
$csv[]=$wo;
$csv[]=$duration;
$csv[]=$startdate;
$csv[]=$enddate;
$csv[]=$asd;
$csv[]=$add;
$csv[]=$aduration;
$csv[]=$transferredto;
$csv[]=$prb;
$csv[]=$note;
$csv[]=$projectname;
$csv[]=$a;
$csv[]=$b;
$csv[]=$c;    

$file = fopen("contacts.csv","w");

foreach ($csv as $line)
{
  fputcsv($csv,$line);
}

fclose($file);

http://php.net/manual/fr/function.fputcsv.php

fputcsv() uses an array as 2nd parameter, not a string. And the 1st parameter must be your file handler.

The following code will create or add to contacts.csv using a comma to delimit each row;

If you wish to add a new line you can open the file using the a+ flag check if it contains a character, if it does add

From PHP manual on a+ Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

// set dummy data
$_POST['txt_number'] = "test";
$_POST['txt_description'] = "test";
$_POST['txt_division'] = "test";
$_POST['txt_stage'] = "test";
$_POST['txt_category'] = "test";
$_POST['txt_priority'] = "test";
$_POST['txt_frequency'] = "test";
$_POST['txt_notapprove'] = "test";
$_POST['txt_approve'] = "test";
$_POST['txt_notexist'] = "test";
$_POST['txt_wo'] = "test";
$_POST['duration'] = "test";
$_POST['startdate'] = "test";
$_POST['enddate'] = "test";
$_POST['txt_asd'] = "test";
$_POST['txt_add'] = "test";
$_POST['txt_aduration'] = "test";
$_POST['txt_transferredto'] = "test";
$_POST['txt_percentage'] = "test";
$_POST['txt_note'] = "test";
$_POST['txt_projectname'] = "test";
$_POST['txt_priority'] = "low pri";

// open the file with a+ flag
$file = fopen('contacts.csv', 'a+');

// read the file and check for a character
if( fread($file, 1) != "" ){

    // file has data, write new line
    fwrite($file, "
");

}

// build csv array
$csv=array();
$csv[] = $_POST['txt_number'];
$csv[] = $_POST['txt_description'];
$csv[] = $_POST['txt_division'];
$csv[] = $_POST['txt_stage'];
$csv[] = $_POST['txt_category'];
$csv[] = $_POST['txt_priority'];
$csv[] = $_POST['txt_frequency'];
$csv[] = $_POST['txt_notapprove'];
$csv[] = $_POST['txt_approve'];
$csv[] = $_POST['txt_notexist'];
$csv[] = $_POST['txt_wo'];
$csv[] = $_POST['duration'];
$csv[] = $_POST['startdate'];
$csv[] = $_POST['enddate'];
$csv[] = $_POST['txt_asd'];
$csv[] = $_POST['txt_add'];
$csv[] = $_POST['txt_aduration'];
$csv[] = $_POST['txt_transferredto'];
$csv[] = $_POST['txt_percentage'];
$csv[] = $_POST['txt_note'];
$csv[] = $_POST['txt_projectname'];
$csv[] = $_POST['txt_priority'];

// set empty output string
$sOutput = "";

// loop through each array entry, concatonating a comma each time if output isnt empty (after first pass)
foreach ($csv as $sLine) {
    if($sOutput != "") $sOutput .= ",";
    $sOutput .= $sLine;
}

// write the output string to the file
fwrite($file, $sOutput);

// close file
fclose($file);

This Writes the following to contacts.csv;

test,test,test,test,test,low pri,test,test,test,test,test,test,test,test,test,test,test,test,test,test,test,low pri

Change from

foreach ($csv as $line)
{
  fputcsv($csv,$line);
}

to

fputcsv($file,$csv);

For multiline use

fputcsv($file,$csv,"
"); 

or

fputcsv($file,$csv,"");

For append in csv use

$file = fopen("contacts.csv","a");