Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    Sunshine State
    Posts
    80
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    dnnhater (01-11-2013)

  • #3
    New Coder
    Join Date
    Jul 2011
    Location
    Sunshine State
    Posts
    80
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •