Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Walter Chilomer

INSERIR ARRAY MULTI-DIMENSIONAL NO MySQL

Recommended Posts

Galera,

Estou tentando gravar multi registros no MySQL, está complicado!!!

 

Tenho a seguinte saida no meu $_POST:

string(11) "0.A.004.491" string(11) "6.B.200.101" string(33) "BULK MASSA BASE BATOM MATTE - DDP" string(6) "17,303" 
string(5) "0.000" string(11) "0.A.004.491" string(11) "2.P.335.101" string(12) "CERAPHYL 791" string(5) "0,346" string(7) "191.800" 
string(11) "0.A.004.491" string(11) "2.P.355.101" string(12) "BENTONE 38 V" string(5) "0,692" string(7) "104.840" 
string(11) "0.A.004.491" string(11) "2.P.075.101" string(44) "FILTRO SOLAR - METOXICINAMATO DE ETIL HEXILA" string(5) "0,087" string(6) "12.222" 
string(11) "0.A.004.491" string(11) "2.P.152.101" string(11) "TINOGARD TL" string(5) "0,035" string(5) "6.132" 
string(11) "0.A.004.491" string(11) "2.P.376.101" string(13) "BENZOFENONA 3" string(5) "0,087" string(6) "27.055" 
string(11) "0.A.004.491" string(11) "2.P.342.101" string(30) "PIG DIOXIDO DE TITANIO ANATASE" string(5) "2,028" string(7) "929.426" 
string(11) "0.A.004.491" string(11) "6.B.200.101" string(33) "BULK MASSA BASE BATOM MATTE - DDP" string(6) "17,303" 
string(5) "0.000" string(11) "0.A.004.491" string(11) "2.P.335.101" string(12) "CERAPHYL 791" string(5) "0,346" string(7) "191.800" 
string(11) "0.A.004.491" string(11) "2.P.355.101" string(12) "BENTONE(11) "0.A.004.491" string(11) "2.P.340.101" string(22) "PIG RED 7 D&C A-502.14" string(5) "0,588" string(7) "157.606" 
string(11) "0.A.004.491" string(11) "2.P.352.101" string(19) "PIG RED 6 D&C A-506" string(5) "0,471" string(6) "60.612" 
string(11) "0.A.004.491" string(11) "2.P.366.101" string(19) "PIG RED 3 D&C A-503" string(5) "0,706" string(7) "186.843" 
string(11) "0.A.004.491" string(11) "2.P.394.101" string(23) "PIG RED 27 D&C A-511.01" string(5) "1,440" string(6) "25.000" 
string(11) "0.A.004.491" string(11) "2.P.365.101" string(23) "PIG VIOLETA DE MANGANES" string(5) "2,087" string(6) "71.794" 
string(11) "0.A.004.491" string(11) "2.P.329.101" string(14) "OLEO DE RICINO" string(5) "6,731" string(7) "798.064" 
string(11) "0.A.004.491" string(11) "2.P.121.101" string(8) "DALPAR L" string(5) "1,038" string(7) "765.400" 
string(11) "0.A.004.491" string(11) "2.P.300.101" string(17) "MAXXI SILK POWDER" string(5) "0,692" string(7) "101.647" 
string(11) "0.A.004.491" string(11) "2.P.339.101" string(11) "AEROSIL 200" string(5) "0,173" string(7) "111.925" 
string(11) "0.A.004.491" string(11) "2.E.005.101" string(35) "ES LAP 205379 BOUQUET CHOCO FLAVOR " string(5) "0,104" string(6) "45.290" 

Correspondente aos seguintes campos:
fo_cod_aca, fo_cod_pro, fo_descri_mp, fo_kg_mp, ee_ID, ee_estoque.

 

Estou tentando gravar conforme esse script:

 

<?php     
    require_once 'sys_init.php';    

    if (isset($_POST)){    

    $fo_cod_aca         = $_POST['fo_cod_aca'];
    $fo_cod_pro         = $_POST['fo_cod_pro'];
    $fo_descri_mp     = $_POST['fo_descri_mp'];
    $fo_kg_mp           = $_POST['fo_kg_mp'];
    $ee_ID                  = $_POST['ee_ID'];
    $ee_estoque       = $_POST['ee_estoque'];
                    
    $ext = implode(', ', $fo_cod_aca);
    $ext = implode(', ', $fo_cod_pro);
    $ext = implode(', ', $fo_descri_mp);
    $ext = implode(', ', $fo_kg_mp);
    $ext = implode(', ', $ee_ID);
    $ext = implode(', ', $ee_estoque);
                                
    for ($i=0; $i<=count($ext); $i++) {
        
        $CALL =     db_connect();
                                
        $stmt = $CALL ->prepare("INSERT INTO tab_for_temp 
            (        
            fo_cod_aca,
            fo_cod_pro,
            fo_descri_mp,
            fo_kg_mp,
            ee_ID,
            ee_estoque
            )                
            VALUES 
            (                                                                              
            fo_cod_aca        ='".$ext[$i]."', 
            fo_cod_pro        ='".$ext[$i]."',                
            fo_descri_mp    ='".$ext[$i]."',
            fo_kg_mp            ='".$ext[$i]."',
            ee_ID                ='".$ext[$i]."',
            ee_estoque        ='".$ext[$i]."'
            )                                                                                        
            ");                                                                                                                        
            $stmt->execute(array($ext[$i]));                    
    }    
    }                
  header('Location: op_fo_escolher_pdo.php');                                                                
?>

 

Só grava uma linha, com os campos preenchidos com  1

 

Agradeço qualquer auxilio.

 

Cordialmente,

Walter

 

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá Walter,

 

Pega o resultado que você quer gravar (array), converte para Json usando o json_encode (http://php.net/manual/pt_BR/function.json-encode.php) e ao consultar, desconverta ele usando json_decode. Acredito que irá resolver! =)

Compartilhar este post


Link para o post
Compartilhar em outros sites
Em 16/09/2017 at 22:02, th4deu disse:

Olá Walter,

 

Pega o resultado que você quer gravar (array), converte para Json usando o json_encode (http://php.net/manual/pt_BR/function.json-encode.php) e ao consultar, desconverta ele usando json_decode. Acredito que irá resolver! =)

Então, th4deu

 

Testei todas as possibilidades, inclusive com json_encode, que grava apenas a última linha. A melhor solução que encontrei, que grava corretamente, tambem, sómente a ultima linha do array:

 

bla,bla,bla....

$inserta = array(  'fo_cod_aca' => "'$cod_aca'",
                                'fo_cod_pro' => "'$cod_pro'",
                                'fo_descri_mp' => "'$descri_mp'",
                                'fo_kg_mp' => "'$kg_qt_cto'",
                                'ee_ID' => "'$ee_ID'",
                                'ee_estoque' => "'$estoque'",                                                    
                             );                                            

 

        {                                                          
        $coluna = array_keys($inserta);           //pega as chaves do array
        $coluna = implode(',',$coluna);             //transforma em string
        $value = implode(',',$inserta);               //transforma valores em string                   

        $stmt = $CALL -> prepare("INSERT INTO tab_for_temp($coluna) VALUES ($value)");
        }                                                         
        }                                                                                                                      
       if ($stmt->execute())
        {            
       header('Location: op_fo_escolher_pdo.php');
        }
      else
        {
       echo "Erro ao cadastrar";
       print_r($stmt->errorInfo());
        }          

          

Abraços. Fique bem.

Walter.

Compartilhar este post


Link para o post
Compartilhar em outros sites
6 horas atrás, Walter Chilomer disse:

Então, th4deu

 

Testei todas as possibilidades, inclusive com json_encode, que grava apenas a última linha. A melhor solução que encontrei, que grava corretamente, tambem, sómente a ultima linha do array:

 

bla,bla,bla....

$inserta = array(  'fo_cod_aca' => "'$cod_aca'",
                                'fo_cod_pro' => "'$cod_pro'",
                                'fo_descri_mp' => "'$descri_mp'",
                                'fo_kg_mp' => "'$kg_qt_cto'",
                                'ee_ID' => "'$ee_ID'",
                                'ee_estoque' => "'$estoque'",                                                    
                             );                                            

 

        {                                                          
        $coluna = array_keys($inserta);           //pega as chaves do array
        $coluna = implode(',',$coluna);             //transforma em string
        $value = implode(',',$inserta);               //transforma valores em string                   

        $stmt = $CALL -> prepare("INSERT INTO tab_for_temp($coluna) VALUES ($value)");
        }                                                         
        }                                                                                                                      
       if ($stmt->execute())
        {            
       header('Location: op_fo_escolher_pdo.php');
        }
      else
        {
       echo "Erro ao cadastrar";
       print_r($stmt->errorInfo());
        }          

          

Abraços. Fique bem.

Walter.

Galera, resolvido,

Script:

 

require_once 'sys_init.php';    

$CALL = db_connect();     

    $qt_bulk =     isset($_POST['fo_kg_mp'])     ? $_POST['fo_kg_mp'] : '';
    
    $cod_aca = isset($_POST['aca_cod_aca'])     ? $_POST['aca_cod_aca'] :null;                      
              
    //JOIN 
    $cto_join         = ("SELECT * FROM t_qt_bulk INNER JOIN tab_estoque ON            t_qt_bulk.fo_cod_pro=tab_estoque.ee_codigo WHERE t_qt_bulk.fo_cod_base ORDER BY fo_fase");                            
    $qt_cto_join     = $CALL->prepare($cto_join);    
    $qt_cto_join    ->execute();    
            
    $le_qto = ("SELECT * FROM t_qt_bulk");
    $st_for = $CALL->prepare($le_qto);
    $st_for->execute();    
    $use_for = $st_for->fetch(PDO::FETCH_ASSOC);                                    
                                                                
    while ($linha = $qt_cto_join->fetch()) {    
        $cod_aca            = $linha['fo_cod_aca'];
        $cod_pro            = $linha['fo_cod_pro'];
        $descri_mp        = $linha['fo_descri_pro'];   
        $cento_for         = $linha['fo_cento_for'];                                   
        $ee_ID                 = $linha['ee_ID'];
        $estoque            = $linha['ee_estoque'];  
       
        $kg_qt_cto         = ($cento_for*$qt_bulk)/100;                                                                                                                                      
        $data = array(
            array(
                "$cod_aca",
                "$cod_pro",
                "$descri_mp",
                "$kg_qt_cto",
                "$ee_ID",
                "$estoque"            

            )                        
        );                              
       
 $stmt = $CALL->prepare("INSERT INTO tab_for_temp    (fo_cod_aca,fo_cod_pro,fo_descri_mp,fo_kg_mp,ee_ID,ee_estoque)   
 VALUES                                         
 ('$cod_aca','$cod_pro','$descri_mp','$kg_qt_cto','$ee_ID','$estoque')                                        

 ");                        
            
        foreach ($data as $row) {                
        $bind = $stmt->bindParam('sssdid',
         $row[0],
         $row[1],
         $row[2],
         $row[3],
         $row[4],
         $row[5]

         );                                                                                                 
                                                                                                                                                    
    if ($stmt->execute())
        {            
       header('Location: op_fo_escolher_pdo.php');
        }
    else
        {
       echo "Erro ao cadastrar";
   print_r($stmt->errorInfo());
        }    
        }    
        //var_dump($row);    
        }                                                    
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites
10 minutos atrás, Walter Chilomer disse:

Galera, resolvido,

Script:

 

require_once 'sys_init.php';    

$CALL = db_connect();     

    $qt_bulk =     isset($_POST['fo_kg_mp'])     ? $_POST['fo_kg_mp'] : '';
    
    $cod_aca = isset($_POST['aca_cod_aca'])     ? $_POST['aca_cod_aca'] :null;                      
              
    //JOIN 
    $cto_join         = ("SELECT * FROM t_qt_bulk INNER JOIN tab_estoque ON            t_qt_bulk.fo_cod_pro=tab_estoque.ee_codigo WHERE t_qt_bulk.fo_cod_base ORDER BY fo_fase");                            
    $qt_cto_join     = $CALL->prepare($cto_join);    
    $qt_cto_join    ->execute();    
            
    $le_qto = ("SELECT * FROM t_qt_bulk");
    $st_for = $CALL->prepare($le_qto);
    $st_for->execute();    
    $use_for = $st_for->fetch(PDO::FETCH_ASSOC);                                    
                                                                
    while ($linha = $qt_cto_join->fetch()) {    
        $cod_aca            = $linha['fo_cod_aca'];
        $cod_pro            = $linha['fo_cod_pro'];
        $descri_mp        = $linha['fo_descri_pro'];   
        $cento_for         = $linha['fo_cento_for'];                                   
        $ee_ID                 = $linha['ee_ID'];
        $estoque            = $linha['ee_estoque'];  
       
        $kg_qt_cto         = ($cento_for*$qt_bulk)/100;                                                                                                                                      
        $data = array(
            array(
                "$cod_aca",
                "$cod_pro",
                "$descri_mp",
                "$kg_qt_cto",
                "$ee_ID",
                "$estoque"            

            )                        
        );                              
       
 $stmt = $CALL->prepare("INSERT INTO tab_for_temp    (fo_cod_aca,fo_cod_pro,fo_descri_mp,fo_kg_mp,ee_ID,ee_estoque)   
 VALUES                                         
 ('$cod_aca','$cod_pro','$descri_mp','$kg_qt_cto','$ee_ID','$estoque')                                        

 ");                        
            
        foreach ($data as $row) {                
        $bind = $stmt->bindParam('sssdid',
         $row[0],
         $row[1],
         $row[2],
         $row[3],
         $row[4],
         $row[5]

         );                                                                                                 
                                                                                                                                                    
    if ($stmt->execute())
        {            
       header('Location: op_fo_escolher_pdo.php');
        }
    else
        {
       echo "Erro ao cadastrar";
   print_r($stmt->errorInfo());
        }    
        }    
        //var_dump($row);    
        }                                                    
?>

RESOLVIDO

Galera obrigado.

Walter.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.