Insert selected records into another table

Asked

Viewed 720 times

0

I need to export data from a table I have in our intranet to a table on the internet, I created a table where I show the events and in this the user can choose the ones that want to export through checkbox selection.

Step(s) id(s) to a page where I do the rescue and search all related data and make a JSON return, I would like the opinion if I am on the right track in the way I am doing this, in case the data export.

See the process page:

<?php 

require_once("../db/conexao.php");

// Recebendo o array com os ID´S
$checkboxes = $_GET['list'];

$arr = array(); 
$arr['flag'] = 0;   

// laço para buscar e-mail e efetuar envio
foreach($checkboxes as $id) {

    // buscando registro(s)
    mysql_select_db($database_conexao, $conexao);
    $query_rsRegistro = "SELECT FROM tecnicoEventos WHERE idEvento = $id ";
    $rsRegistro = mysql_query($query_rsRegistro, $conexao) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($rsRegistro);

    // resgatando valores da seleção
    $IdEvento = $row_rsRegistro['idEvento'];
    $IdUnidade = $row_rsRegistro['idUnidade'];
    $Data = $row_rsRegistro['data'];
    $Nome = $row_rsRegistro['nome'];
    $Evento = $row_rsRegistro['evento'];
    $Publico = $row_rsRegistro['publico'];
    $Participantes = $row_rsRegistro['participantes'];
    $Local = $row_rsRegistro['local'];
    $Horario = $row_rsRegistro['horario'];
    $Carga = $row_rsRegistro['carga'];
    $Status = $row_rsRegistro['status'];
    $Responsavel = $row_rsRegistro['responsavel'];
    $DataCadastro = $row_rsRegistro['dataCadastro'];
    $AtivoSite = $row_rsRegistro['ativoSite'];
    $Img = $row_rsRegistro['img'];
    $TumbImg = $row_rsRegistro['tumbImg'];
    $Atualizado = $row_rsRegistro['atualizado'];        

        if ($totalRows_rsRegistro > 0) {

            // Conectando ao banco de dados e realizando a inserção de dados
            require_once('Connections/conWeb.php');;

            /* Inserindo dados no banco de dados */ 
            mysql_select_db($database_conWebCocari, $conWebCocari);
            $sqlCand = "INSERT INTO tecnicoEventos ( 
                idEvento, 
                idUnidade, 
                data, 
                nome, 
                evento, 
                publico, 
                participantes, 
                local, 
                horario, 
                carga, 
                status, 
                responsavel, 
                dataCadastro, 
                ativoSite, 
                img, 
                tumbimg, 
                atualizado ) 
            VALUES (
                $IdEvento, 
                $IdUnidade, 
                '$Data', 
                '$Nome',                            
                '$Evento', 
                '$Publico', 
                $Participantes, 
                '$Local', 
                '$Horario', 
                '$Carga', 
                $Status, 
                '$Responsavel', 
                '$DataCadastro', 
                $AtivoSite, 
                '$Img', 
                '$TumbImg', 
                '$Atualizado')";                                
            $resultInsert = mysql_query($sqlCand, $conWeb) or die ("Erro Inserindo Registro: " . mysql_error());    

        }

        $resultado = $resultInsert;

} // fim do foreach



if ($resultado == 1) {
    $arr['result'] = 'TRUE';
    $arr['msg'] = 'Os registros foram inseridos com sucesso';       
    $arr['flag'] = 1;   
} else {
    $arr['result'] = 'FALSE';
    $arr['msg'] = 'Os registros não foram inseridos, por favor verifique';      
    $arr['flag'] = 0;       
}

$arr = array_map('htmlentities',$arr);  
echo json_encode($arr);     

?>

  • All select fields will be inserted in the other table?

  • That’s right, every field.

  • See if my answer solves your case.

1 answer

3


A solution would be to use INSERT with SELECT.

Example:

INSERT INTO banco.tabela-destino (campo1, campo2, campo3...)
SELECT campo1,campo2,campo3... FROM banco.tabela-origem;

Note that the fields are in the same sequence in both the INSERT statement and the SELECT statement because the sequence of fields in both instructions is followed. Also do not forget the semicolon at the end of the last instruction. Essential.

Source: [1]

  • 1

    +1, since you want to put only user-selected events, pass the event ids and use IN in your query.

  • How I will connect in two different databases how can I do this using the example of Insert with Select?

  • Thanks to @NULL and gcarvalho97 for their contributions, they helped me a lot.

Browser other questions tagged

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