Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 03:15 PM   PM User | #1
dnnhater
New Coder

 
Join Date: Jul 2011
Location: Sunshine State
Posts: 79
Thanks: 18
Thanked 0 Times in 0 Posts
dnnhater is an unknown quantity at this point
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!
dnnhater is offline   Reply With Quote
Old 01-11-2013, 03:22 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
dnnhater (01-11-2013)
Old 01-11-2013, 03:49 PM   PM User | #3
dnnhater
New Coder

 
Join Date: Jul 2011
Location: Sunshine State
Posts: 79
Thanks: 18
Thanked 0 Times in 0 Posts
dnnhater is an unknown quantity at this point
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!
dnnhater is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:16 PM.


Advertisement
Log in to turn off these ads.