CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Warning: fwrite() expects parameter 3 to be long, string (http://www.codingforums.com/showthread.php?t=285623)

dnnhater 01-11-2013 03:15 PM

Warning: fwrite() expects parameter 3 to be long, string
 
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!

Fou-Lu 01-11-2013 03:22 PM

That sure won't work. Fwrite signature is int fwrite(resource, string, int);. There is no $columns type option, that would be a single line within it.
You should be using fwrite($handle, $csvData);, and either omitting the third parameter or giving it a maximum length to work with. So simply write the $columns to the file first, then write the data. Looks like you probably only want the columns if the file is empty so you can always check the size first.
fputcsv is an alternative. It was designed to create csv data from an array. You may want to consider that option instead.

dnnhater 01-11-2013 03:49 PM

thanks for that Fou-Lu!

I feel so dumb - when I was trying to google the error yesterday I was reading the error incorrectly so I never even looked up fwrite on php.net - mostly because I knew that only the $columns values was the only thing that was different from what I'd used in the past

I have now learned that the 3rd parameter is indeed for an (int) value - yes I went back to php.net which I should have done from the gitgo...

I'm looking up fputcsv now

thank you for your time!


All times are GMT +1. The time now is 04:14 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.