Help! PHP + Mysql import

Asked

Viewed 49 times

0

I need some help, I created an import code for XML to the MYSQL via PHP.

Follows code:

<?php

if (!isset($seg)) {
    exit;
}
//$dados = $_FILES['arquivo'];

if (!empty($_FILES['arquivo']['tmp_name'])) {

$DOMDocument = new DOMDocument( '1.0', 'UTF-8' );
$DOMDocument->preserveWhiteSpace = false;

$DOMDocument->load($_FILES['arquivo']['tmp_name']);


$products = $DOMDocument->getElementsByTagName("Row");


foreach($products as $product){


      $cpf = $product->getElementsByTagName("Data")->item(0)->nodeValue;
      $nome_cliente = $product->getElementsByTagName("Data")->item(1)->nodeValue;  
      $telefone = $product->getElementsByTagName("Data")->item(2)->nodeValue;  
      $dataemissao = $product->getElementsByTagName("Data")->item(3)->nodeValue;
      $vencimento = $product->getElementsByTagName("Data")->item(4)->nodeValue;
      $funcionario = $product->getElementsByTagName("Data")->item(5)->nodeValue;
      $adesao_mensalidade = trim($product->getElementsByTagName("Data")->item(6)->nodeValue);
      $forma_envio = $product->getElementsByTagName("Data")->item(7)->nodeValue;
      $data_liquidacao = $product->getElementsByTagName("Data")->item(8)->nodeValue;


      $result_import = "INSERT INTO adms_liquida_boletos_plano (cpf, nome_cliente, telefone, emissao, vencimento, vendedor, adesao_mensalidade, forma_envio, data_liquidacao, inserido) VALUES ('$cpf','$nome_cliente','$telefone','$dataemissao','$vencimento','$funcionario','$adesao_mensalidade', '$forma_envio','$data_liquidacao', NOW())";
      $resultado_import = mysqli_query($conn, $result_import);

  } if (mysqli_insert_id($conn)) {
            $_SESSION['msg'] = "<div class='alert alert-success'>Notas importadas com sucesso! <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div>";
            $url_destino = pg . '/importar/import_liquid_plano';
            header("Location: $url_destino");
        }else{

         $_SESSION['msg'] = "<div class='alert alert-danger'>ERRO: Notas não foram importadas! <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div>";
         $url_destino = pg . '/importar/import_liquid_plano';
         header("Location: $url_destino");

     }



  }

However, there is an error that imports blank cells, I would like to know how to make sure that this problem does not happen.

  • If you can send an example xml, it might be easier to find other ways to solve, I’ve already handled XML but without using Domdocument.

1 answer

0

If you notice some blank cells, however, I do not want them to be imported into the bank. Follow example:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Call Center</Author>
<LastAuthor>Davi Lima</LastAuthor>
<Created>2017-04-06T15:11:49Z</Created>
<LastSaved>2019-01-08T10:39:47Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>8955</WindowHeight>
<WindowWidth>20730</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>1125</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s69">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Bold="1"/>
</Style>
<Style ss:ID="s71">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Color="#000000"/>
</Style>
<Style ss:ID="s72">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="Calibri" x:Family="Swiss"/>
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s74">
<Borders/>
</Style>
<Style ss:ID="s90">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s91">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
</Style>
</Styles>
<Worksheet ss:Name="Plan1">
<Table ss:ExpandedColumnCount="10" ss:ExpandedRowCount="5" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
<Column ss:Width="63"/>
<Column ss:AutoFitWidth="0" ss:Width="201"/>
<Column ss:Width="69"/>
<Column ss:AutoFitWidth="0" ss:Width="53.25"/>
<Column ss:AutoFitWidth="0" ss:Width="69"/>
<Column ss:AutoFitWidth="0" ss:Width="93.75"/>
<Column ss:AutoFitWidth="0" ss:Width="76.5"/>
<Column ss:AutoFitWidth="0" ss:Width="47.25"/>
<Column ss:Width="90.75"/>
<Row>
<Cell ss:StyleID="s71">
<Data ss:Type="Number">12345678910</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">ADILSON PEREIRA</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">21 99152-1127</Data>
</Cell>
<Cell ss:StyleID="s72">
<Data ss:Type="DateTime">2019-01-03T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s72">
<Data ss:Type="DateTime">2019-01-04T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">RAPHAEL DE SOUZA</Data>
</Cell>
<Cell ss:StyleID="s69">
<Data ss:Type="String">NÃO</Data>
</Cell>
<Cell ss:StyleID="s69">
<Data ss:Type="String">EMAIL</Data>
</Cell>
<Cell ss:StyleID="s90">
<Data ss:Type="DateTime">2019-02-26T00:00:00.000</Data>
</Cell>
</Row>
<Row>
<Cell ss:StyleID="s71">
<Data ss:Type="Number">12345678910</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">RAIMUNDO DE SOUZA LOPES</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">21 99125-0617</Data>
</Cell>
<Cell ss:StyleID="s72">
<Data ss:Type="DateTime">2019-01-03T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s72">
<Data ss:Type="DateTime">2019-01-10T00:00:00.000</Data>
</Cell>
<Cell ss:StyleID="s71">
<Data ss:Type="String">GABRIEL FARIAS</Data>
</Cell>
<Cell ss:StyleID="s69">
<Data ss:Type="String">NÃO</Data>
</Cell>
<Cell ss:StyleID="s69">
<Data ss:Type="String">EMAIL</Data>
</Cell>
<Cell ss:StyleID="s90">
<Data ss:Type="DateTime">2019-02-27T00:00:00.000</Data>
</Cell>
</Row>
<Row>
<Cell ss:StyleID="s71"/>
<Cell ss:StyleID="s71"/>
<Cell ss:StyleID="s71"/>
<Cell ss:StyleID="s72"/>
<Cell ss:StyleID="s72"/>
<Cell ss:StyleID="s71"/>
<Cell ss:StyleID="s69"/>
<Cell ss:StyleID="s69"/>
<Cell ss:StyleID="s91"/>
</Row>
<Row>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
</Row>
<Row>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
<Cell ss:StyleID="s74"/>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.31496062000000002"/>
<Footer x:Margin="0.31496062000000002"/>
<PageMargins x:Bottom="0.78740157499999996" x:Left="0.511811024" x:Right="0.511811024" x:Top="0.78740157499999996"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<VerticalResolution>0</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>8</ActiveRow>
<ActiveCol>3</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Plan2">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"> </Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.31496062000000002"/>
<Footer x:Margin="0.31496062000000002"/>
<PageMargins x:Bottom="0.78740157499999996" x:Left="0.511811024" x:Right="0.511811024" x:Top="0.78740157499999996"/>
</PageSetup>
<Visible>SheetHidden</Visible>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Plan3">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"> </Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.31496062000000002"/>
<Footer x:Margin="0.31496062000000002"/>
<PageMargins x:Bottom="0.78740157499999996" x:Left="0.511811024" x:Right="0.511811024" x:Top="0.78740157499999996"/>
</PageSetup>
<Visible>SheetHidden</Visible>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.