0

I'm developing an app where user upload excel [.xlsx] file for dumping data into MySQL database. I have programmed in such a way that there is a LOG created for each import. So that user can see if there is any error occurred and etc.. My script was working perfectly before implementing the log system.

After implementing the log system i can see duplicate rows inserted into database. Also die() command is not working.

It just keep looping continuously!

I have written sample code below. Please tell whats wrong in my logging method.

Note: if i remove logging [Writing into file] script works correctly.

$file = fopen("20131105.txt", "a");
        fwrite($file, "LOG CREATED".PHP_EOL);

        foreach($hdr as $k => $v) {
                $username = $v['un'];
                $address  = $v['adr'];
                $message  = $v['msg'];

                if($username == '') {
                    fwrite($file, 'Error: Missing User Name'.PHP_EOL);
                    continue;
                } else {
                     // insert into database
                 }
            }

        fwrite($file, PHP_EOL."LOG CLOSED");
        fclose($file);
        echo 1;
        die();
sravis
  • 3,562
  • 6
  • 36
  • 73

2 Answers2

0

First, your die statement is after your loop. It needs to be inside your loop to end it;

Second, you're looping over $hdr. It's not defined in your snippet tho. It has to be an array. What does it contain?

var_dump($hdr);
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • $hdr is an associative array. I'm sure there is no duplicates in $hdr array. My script loops through $hdr array and checks if any data is missing. If there is any data missing it will just log "Data is missing etc.." and skips instead of inserting it, command [continue]. die() command is written at end to ensure that script dies after completing the process – sravis Nov 05 '13 at 05:10
0

The documentation for foreach as given in php manual highlights


"Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()."[1].


Try unsetting the values in foreach using unset($value) . This might be the reason for duplicate values.

geekgugi
  • 389
  • 1
  • 6
  • 22