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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Writing $_POST data to excel file

    Hello,

    I currently have a working script that takes data from a POST submission and adds to an an excel file (I didn't make the script).

    I'd like to simply take this working script (on newsletter.php) and use it on another page (tellafriend.php) to write to another xls file. For some reason, it won't work on tellafriend.php.

    Here's the original script (newsletter.php):

    PHP Code:
         /*------ COMPOSE/BUILD DATA ---------*/
         
    $data $_POST['name'] . "\t" $_POST['email'] . "\t" $_POST['street'] . "\t" $_POST['city'] . "\t" $_POST['state'] . "\t" $_POST['zip'] . "\t" $_POST['identification'];

         
    $file='/PATH/spreadsheet/newsletter.xls';
         
    write_to_file($file,$data); 
    here's the script I've added to tellafriend.php:

    PHP Code:
        /*------ COMPOSE/BUILD DATA ---------*/

         
    $data $_POST['name'] . "\t" $_POST['youremail'] . "\t" $_POST['friendemail'];

         
    $file='/PATH/spreadsheet/tellafriend.xls';
         
    write_to_file($file,$data); 
         
         echo 
    "debug information: <br /> \$data = $data <br /> \$file = $file <br />"
    I've placed both xls files in the same exact directory. I've also made sure that they both have permissions set to 777. When I run tellafriend.php, the $file and $data variables have the correct information in them.

    Everything seems to be working right... the script runs, the variables are populated, no errors occur (not even in the error log). But for some reason, it doesn't write to the tellafriend.xls file!

    Any ideas? Thanks you!

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    This looks like a function: write_to_file($file,$data);

    Do you know where that function is located, and you can show it to us?

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Ah, I had assumed that write_to_file() was a basic php function, not a custom one.

    I found the function. Here's the original:
    PHP Code:
    function write_to_file($f='data.xls'$data='')
    {
        
    $file $f;
        
    $num_records=0;

        if(
    is_readable($file))
        {
           
    $records=file($file);
           
    $fp fopen($file"a+t");
           if (
    flock($fp2))
           {
             
    // do an exclusive lock
             
    foreach($records as $record)
             {
               
    $num_records++;
             }

             if(
    is_writable($file))
             {
               if(
    $num_records 1)
               {
                 
    fwrite($fp"First Name\tEmail Address\tStreet Address\tZip Code\tState\tIdentification\n");
                 
    fwrite($fp$data "\n");
               }
               else
               {
                 
    fwrite($fp$data "\n");
               }
             }

             
    flock($fp3); // release the lock
           
    }
           
    fclose($fp);
        }

    I've changed tellafriend.php to call write_to_file2() and modified the original:
    PHP Code:
    function write_to_file2($f='data.xls'$data='')
    {
        
    $file $f;
        
    $num_records=0;

        if(
    is_readable($file))
        {
           
    $records=file($file);
           
    $fp fopen($file"a+t");
           if (
    flock($fp2))
           {
             
    // do an exclusive lock
             
    foreach($records as $record)
             {
               
    $num_records++;
             }

             if(
    is_writable($file))
             {
               if(
    $num_records 1)
               {
                 
    fwrite($fp"Name\tEmail\tFriend's Email\n");
                 
    fwrite($fp$data "\n");
               }
               else
               {
                 
    fwrite($fp$data "\n");
               }
             }

             
    flock($fp3); // release the lock
           
    }
           
    fclose($fp);
        }

    The only difference is that the table headers have changed. This makes no difference, since the headers are already set, and this condition is only called when $num_records<1, right?

    I'm not sure what data.xls is... I can't seem to find it on the server anywhere.

    Any thoughts? Thank you for your help!

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    I wonder if somewhere else, "data.xls" is being used,
    and put into "newsletter.xls". I can't figure out any
    other way that "newsletter.xls" is working.

    When you do "tellafriend.xls", is it affecting "newsletter.xls"?

    very strange indeed.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Well at least I'm not the only one that's confused...

    No, the tellafriend.php has absolutely no affect on newsletter.xls, event when it's using the exact same function!

    newletter.php still works! It adds to the excel file. I can't figure it out at all. And I'm sure that data.xls does not exist on the server. Wouldn't have to exist in the same directory anyway?

    It doesn't even make sense that $f=data.xls. Does that mean it's ignoring the $file variable that's being passed into it? I've tried to replace $f=data.xls with just $f so that it uses the $file variable that's given to it, but it just does nothing. Weird that it wouldn't even give an error...

  • #6
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    I'm hoping someone else sees something that we're missing ... I just can't see it.

    Just for kicks, post the complete "newsletter.php" script.


    .
    Last edited by mlseim; 01-27-2010 at 11:24 PM.

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Just wanted to let you know that I got it working.

    I couldn't find any problems in the code, so I guessed that there might be some hidden aspect of newletter.xls that was affecting the script.

    Even though the format and permissions were identical, I decided to take newsletter.xls, duplicate it, remove the content, change the headers, rename it to tellafriend.xls, and upload it to the server.

    I ran the script and Voila! It worked!

    I have no idea why.
    Last edited by madmatter23; 02-11-2010 at 08:05 PM.


  •  

    Posting Permissions

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