Question

I'm trying to get form data from an HTML form to be added into a serverside excel csv file. When I try creating the csv file, and writing to it with $_POST values from the HTML form, I get an error saying: "Parse error: syntax error, unexpected T_STRING, expecting ')' in process.php on line 46". I'e tried a few alterations of the following code to see if I could get it to work, but nothing so far. Any help would be very appreciated.

Here's the code:

<?php
$list = array (
    array('$_POST['some_field']', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>
Was it helpful?

Solution

Way too many quotes in there, you don't need them around the $_POST variable.

$list = array (
    array($_POST['some_field'], 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

You can append to the file by using 'a' instead of 'w' in your fopen command:

$fp = fopen('file.csv', 'a');

See the manual here.

OTHER TIPS

Try,

$some_field = $_POST['some_field'];
$list = array (
    array($some_field, 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('aaa', 'bbb')
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top