Skip to content
Advertisement

Insert Excel datas on mysql with php

I’m trying to insert excel datas in mysql with php, I have a code but it’s inserting only the first line off the archive and my archive has 3 lines.

Someone knows how to help me?

JavaScript

Advertisement

Answer

Your script has the following happening:

You set $countlinha = 1;

Which means the condition if ($countlinha > 1) is false on first iteration of foreach($linhas as $linha). I.e. the first ‘record’ inside $linhas is not processed.

Then you increment $countlinha++; it becomes 2

So on second iteration of foreach($linhas as $linha) the code inside the if ($countlinha > 1) is executed.

The second ‘record’ inside $linhas is echoed and written to database.

However, return $stmt; now terminates your script.

So all in all you only got one ‘record’ processed.

TLDR

If you want to get all ‘records’ inside $linhas processed, you should drop the if ($countlinha > 1) condition, and do NOT return inside the loop as it will terminate your script prematurely.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement