将html输入写入csv文件

时间:2016-09-26 11:38:27

标签: php csv

我尝试使用php从html表单写入csv文件。我有一个问题,即文件中没有写入任何内容。

我希望是这样的:

  

A,B,C,d

如果我有一个空输入,它将是这样的

  

一个,, B,C,d

这是代码,它有什么问题?

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

3 个答案:

答案 0 :(得分:1)

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

fputcsv()使用数组作为第二个参数,而不是字符串。 第一个参数必须是你的文件处理程序。

答案 1 :(得分:0)

以下代码将使用逗号分隔每行创建或添加到contacts.csv;

如果你想添加一个新行,你可以使用a + flag检查文件是否包含一个字符,如果它确实添加了\ n

从PHP手册开始,用于阅读和写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。在此模式下,fseek()仅影响读取位置,始终附加写入。

// 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, "\n");

}

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

这将以下内容写入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

答案 2 :(得分:0)

改变
mysqli_real_escape_string

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

用于多行使用

fputcsv($file,$csv);

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

用于附加csv使用

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