Duplicate Mass Records - Run a query for each displayed Record

0

I have a table where only accounts with their type = 'fixed' are displayed

WhatIwouldliketoaccomplishistorunaqueryandduplicateallrecordsinthetableatonce.AtthemomentIonlymanagedtoduplicatetheaccountsonebyoneandnotallresultsdisplayed,whichalreadyhelpsbutdoesnotsolvemyproblemhehehe.

I'mrunningthefollowingquery:

SELECTDate_Vencer,Name,Assignor,Cost_center,Purpose,ValueFROMtab_contactsWHEREID='{$(){$tSqlDup=$tPdo->prepare("INSERT INTO Accounts (Date_Credit, Name, Assignor, CostCost, Purpose, Value) tGet} '");

$ tSqlDup -> execute ();

The idea of this is to remove the work of the user by re-releasing accounts that will have exactly the same data, perhaps differentiating maturity and value ... and clicking the button all values of the fixed type are duplicated ...

I'll leave below the entire HTML structure below:

<table class="table">
    
 <thead>
        
    <tr style="background-color: #428bca;">
                        
        <th>Vencimento</th>

        <th>Nome da Conta</th>

        <th>Cedente</th>

        <th>Finalidade</th>

        <th>CC</th>

        <th>Valor da Ultima Conta</th>

        <th>Duplicar</th>

    </tr>

</thead>
      
<tbody>

<?php
 
require_once("includes/BaseDeDados.php");
require_once("includes/LogSistema.php");
           
    $tQuantidade = 100;
    $tPagina     = (isset ($_GET['pg'])) ? (int)$_GET['pg'] :1;
    $tIniciar    = ($tQuantidade * $tPagina) - $tQuantidade;

    $tSql = $tPdo->prepare("SELECT * FROM Contas WHERE Tipo='F' ORDER BY ID DESC LIMIT $tIniciar, $tQuantidade");
$tSql ->execute();
      
      while($tMC  = $tSql->fetch(PDO::FETCH_ASSOC))
      {   

      //Faz o explode da Data para transformar data do formato US para o formato BR
      $data = $tMC['Data_Vencimento'];
      $data_nova = explode("-",$data);
      // Calcula Mês atual + 1
      $mes = strtotime("+1 month");

      echo "
        <tr>
            
          <td>$data_nova[2]/".date('m', $mes)."/$data_nova[0]</td>

          <td><a href='?controle=***ARQUIVO***&CodC=".codificarUrl($tMC['ID'])."' rel='tooltip' title='Editar Esta Conta'>".utf8_encode($tMC['Nome'])."</td>

          <td>{$tMC['Cedente']}</td>

          <td>{$tMC['Finalidade']}</td>

          <td>{$tMC['Centro_Custo']}</td>

          <td>R$ {$tMC['Valor']}</td>
          
<!--- ////////////////////////////
ESTE E O BOTAO QUE RODA A CONSULTA PARA DUPLICAR A CONTA COM O ID ESCOLHIDO, POREM GOSTARIA DE AO INVES TER DE FAZER ISTO DE CONTA EM CONTA, REALIZAR EM TODAS DE UMA VEZ...
/////////////////////////////////-->
          <td><a href='?controle=***ARQUIVO***&Acao=Duplicar&CodC=".codificarUrl($tMC["ID"])."' role='button' rel='tooltip' title='Duplicar este Arquivo'></a></td>
          
        </tr>";

      }
        
  if (isset($_GET['Acao']) && isset($_GET['CodC']))
    {

# É AQUI AONDE É RODADA A CONSULTA QUE FAZ A DUPLICAÇÃO DA CONTA

      $tGet    = decodificarUrl($_GET['CodC']);
      $tSqlDup = $tPdo->prepare("INSERT INTO Contas (Data_Vencimento, Nome, Cedente, Centro_Custo, Finalidade, Valor) SELECT Data_Vencimento, Nome, Cedente, Centro_Custo, Finalidade, Valor FROM tab_contas WHERE ID = '{$tGet}'");
     
      $tSqlDup ->execute();       
    
    }

?>

<tbody>
    
</table>

PS: In the SS has the buttons to the right of the table, and the button above the table, the above table does not work, it would be the button that would duplicate everything at once, while the buttons to the right of each Account are the buttons that duplicate one at a time ...

    
asked by anonymous 17.04.2017 / 21:24

1 answer

0

You can continue to use your query that does one insert at a time, just putting it inside a loop that passes through all the items.

$query_itens = $tPdo->prepare("SELECT ID FROM tab_contas WHERE SUA_CONDICAO_AQUI")->execute();
$itens = $query_itens->fetchAll();

$tSqlDup = $tPdo->prepare("INSERT INTO Contas (Data_Vencimento, Nome, Cedente, Centro_Custo, Finalidade, Valor)SELECT Data_Vencimento, Nome, Cedente, Centro_Custo, Finalidade, Valor FROM tab_contas WHERE ID = ?");

foreach($itens as $item) {
    $tSqlDup->execute([$item->id]);
}

Just change the condition of the first query to match your case.

    
17.04.2017 / 21:52