0

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);
11
  • You arent pointing fputcsv to the file handle. Try to change fputcsv($csv, $line); to fputcsv($file, $line); Commented Sep 26, 2016 at 11:42
  • 1
    fputcsv($csv,$line); should be fputcsv($file, $line); and you don't need the foreach loop... just do: fputcsv($file, $csv); directly. Commented Sep 26, 2016 at 11:43
  • You should also get an "undefined variable" every time you run this script, since you're always using $a, $b and $c but you only define one of them (which depends on the contents of $exist). Commented Sep 26, 2016 at 11:46
  • @MagnusEriksson i put it like this $file = fopen("contacts.csv","w"); fputcsv($file,$line); fclose($file); but also i have an empty file Commented Sep 26, 2016 at 11:49
  • What does $line come from? Send in your array $csv instead. Commented Sep 26, 2016 at 11:51

3 Answers 3

1

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.

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

Comments

0

Change from

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

to

fputcsv($file,$csv);

For multiline use

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

or

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

For append in csv use

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

5 Comments

i have 2 problem first problem that i have a select input and the select input value in csv file have " " ex: " a " the second the i can't enter multi line in the csv file if i submit the form the values be in csv file if i enter another input and submit the form the first be erase and the second appear
@mhmd solution added for your both problem
replace \n with \r and check
so you have to apply it with every field like fputcsv($file,$fieldname,'\r')
0

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 \n

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

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

10 Comments

i have 2 problem first problem that i have a select input and the select input value in csv file have " " ex: " a " the second the i can't enter multi line in the csv file if i submit the form the values be in csv file if i enter another input and submit the form the first be erase and the second appear
Problem 1; so you have a select box that will result in the value a,b or c coming through? Could I have the name given to the select? Problem 2: Can you confirm the issue is, on resubmitting the form it looses the data in the csv file created?
<select id="selectbasic" name="txt_priority" class="form-control"> <option value=""></option> <option value="Low Priority">Low Priority</option> <option value="Routine">Routine</option> <option value="Urgent">Urgent</option> <option value="Important">Important</option> </select>
if i want to enter another line to csv file using the html form i can't the first line be deleted and the second line appear
the problem is if i use \r or \n the "," disappear and i need this
|

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.