hi gang - I'm getting the following error when handling my form:
Warning: fwrite() expects parameter 3 to be long, string...on line 31
what I'm trying to do is submit my form values into a csv file and I was hoping that I could add column names - I create csv's with the majority of the code below all the time but I've never tried to add column names before so I don't know if maybe I'm not supposed to have 3 parameters in fwrite, or if $columns should be formed differently...or something else altogether?
as you can see, I echo out the $date and $frequency and those values display just fine, as do the $_POST values
PHP Code:
<?php
function protect($string) {
$string = trim($string);
return $string;
}
if (isset($_POST['form_submit']) && ($_POST['submitForm'] == 1)) {
$errors = array();
foreach ($_POST['frequency'] as $key=> $value) {
$freq = "$value, ";
}
$freq = substr($freq,0,-2);
$date = date('n/j/Y');
echo '<pre>';
echo 'Frequency: '.$freq.'<br />';
echo 'Date: '.$date.'<br />';
echo '</pre>';
$columns = 'Date, Pharmacy Store #, Agent Name, Address, Store Mgr Cooperative, Not Cooperative, Senior Traffic, Approaches, Frequency, Kiosk, Problems';
$q1 = protect($_POST['pharmacy_id']);
$q2 = protect($_POST['agent_name']);
$q3 = protect($_POST['agent_address'].', '.$_POST['agent_city'].', '.$_POST['agent_state'].', '.$_POST['agent_zip']);
$q4 = protect($_POST['store_cooperative']);
$q5 = protect($_POST['senior_traffic']);
$q6 = protect($_POST['approaches']);
$q7 = protect($freq);
$q8 = protect($_POST['kiosk']);
$q9 = protect($_POST['problems']);
$filename = 'data/data.csv';
$csvData = "\"".$date."\",\"".$q1."\",\"".$q2."\",\"".$q3."\",\"".$q4."\",\"".$q5."\",\"".$q6."\",\"".$q7."\",\"".$q8."\",\"".$q9."\"\n";
$handle = fopen($filename,"a");
if ($handle) {
fwrite($handle,$columns,$csvData);
fclose($handle);
//header("Location: ".WEBSITE_URL."/thank-you.php");
}
else {
$errors['filenotupdated'] = 'There was an error writing to the file';
}
}
?>
the $errors['filenotupdated'] does not trip, but I'm not sure if that's because I have my $_POST and other variables echoing
any help is greatly appreciated!