0

I'm wanting to store basic data from a single form box and I've created this php code base, but it doesn't seem to be working. Can someone take a glance and see if anything stands out to why this wouldn't work. Edit: the csv file never updates with new data

if (isset($_POST["submit"])) 
{ 
    $name = $_POST["name"]; 
    date_default_timezone_set('America/New_York');
    $date = date('Y-m-d H:i:s');

    if (empty($name)) 
    { 
        echo "ERROR MESSAGE"; 
        die; 
    } 
    $cvsData ='"$name","$date"'.PHP_EOL; 
    $cvsData .= "\"$name\",\"$date\"".PHP_EOL; 
    $fp = fopen("emailAddressses.csv", "a");

    if ($fp) 
    { 
        fwrite($fp,$cvsData); // Write information to the file 
        fclose($fp); // Close the file 
    }
}
6
  • What does 'it doesn't seem to be working' mean? Does it do nothing? Something, but not what you expect? Commented May 24, 2013 at 18:46
  • Would be nice to know what "doesn't seem to be working" means. Error, empty file...? Commented May 24, 2013 at 18:46
  • Is there any error ? Could you put 'error_reporting (E_ALL);' and recheck ? Commented May 24, 2013 at 18:46
  • 3
    Code seems fine if both submit & name are set, I'd recommend fputscv, but for anything more than "it doesn't seem to be working" we need a more exact error. Commented May 24, 2013 at 18:47
  • PHP has built-in CSV handling functions. Any reason why you aren't using them? Commented May 24, 2013 at 19:05

3 Answers 3

7

Use the nicer way in php : fputcsv

Otherwise you need to do lot of error handling to achieve in your case.

$list = array (
    array('First Name', 'Last Name', 'Age'),
    array('Angelina ', 'Jolie', '37'),
    array('Tom', 'Cruise', '50')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

I haven't started error trapping because I'm testing and not getting the file to submit was my first problem before hardening, but thanks I'm going down this route now.
3

You should look into fputcsv. This will add CSV to you file and take care of fields and line ends.

fputcsv($fp,array(array($name,$date)));

You can also specify delimiters and such if you want.

1 Comment

thanks I wasn't aware of fputcsv which is a much cleaner way to accomplish this task
0

This part will not behave like you expect, the variables are not evaluated when inside single quotes:

$cvsData ='"$name","$date"'.PHP_EOL;

You will need to use double quotes:

$cvsData ="\"$name\",\"$date\"".PHP_EOL; 

2 Comments

it wasn't me, but since he's got two lines going into the CSV, with different quote styles, my guess is that he's aware of that and intending the first line (with the single quotes) to be a header line that has the variable name as headers. I agree it's not clear what the intention is, but it is clear from the two lines that he is aware of how quotes work.
@Spudley correct I'm going for headers in the csv, thanks though...I'm a JS guy still understanding the php syntax and usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.