How to pass data from one table to another when one has a relationship?

Asked

Viewed 138 times

0

I created a table by Migration that has relationships and have a table with the same fields (except the incremental id) that has the data. I need to pass the data from the table that has the data to the table that has the relationships. I made a script for this but gives foreignkey error. Do you have another way to copy it? Obg.

Edit

Structure Original table with data:

CREATE TABLE IF NOT EXISTS `locais` (
  `CODIGOLOCAL` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `EMPRESA` int(11) NOT NULL,
  `DESCRICAO` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `CODIGOASCEDENTE` varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CODIGORESULT` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DATAATIVO` date NOT NULL,
  `RESPONSAVEL` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DATADESATIVADO` date DEFAULT NULL,
  `CODIGOTIPO` varchar(9) COLLATE utf8_unicode_ci DEFAULT NULL,
  `TELEFONE` varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CENTROCUSTO` int(5) DEFAULT NULL,
  `EMAIL` varchar(70) COLLATE utf8_unicode_ci DEFAULT NULL,
  `ENDERECO` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `COMPLEMENTO` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `BAIRRO` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CIDADE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Table structure created with Migration and relationships:

Table structure localsoriginal

CREATE TABLE IF NOT EXISTS `localsoriginal` (
  `id` int(10) unsigned NOT NULL,
  `codigolocal` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `descricaolocal` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `idempresa` int(10) unsigned NOT NULL,
  `codigopai` int(10) unsigned NOT NULL,
  `result` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `dataativo` date NOT NULL,
  `datadesativado` date NOT NULL,
  `idresponsavel` int(10) unsigned NOT NULL,
  `idtipolocal` int(10) unsigned NOT NULL,
  `telefone` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `centrocusto` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `endereco` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `complemento` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `birro` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cidade` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `iduf` int(10) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Indexes for dumped Tables

Indexes for table localsoriginal

ALTER TABLE `localsoriginal`
  ADD PRIMARY KEY (`id`), ADD KEY `locals_idempresa_foreign` (`idempresa`), ADD KEY `locals_codigopai_foreign` (`codigopai`), ADD KEY `locals_idresponsavel_foreign` (`idresponsavel`), ADD KEY `locals_idtipolocal_foreign` (`idtipolocal`), ADD KEY `locals_iduf_foreign` (`iduf`);

AUTO_INCREMENT for dumped Tables

AUTO_INCREMENT for table localsoriginal

ALTER TABLE `localsoriginal`
  MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;

Constraints for dumped Tables

Limiters for the table localsoriginal

ALTER TABLE `localsoriginal`
ADD CONSTRAINT `locals_codigopai_foreign` FOREIGN KEY (`codigopai`) REFERENCES `pais` (`id`),
ADD CONSTRAINT `locals_idempresa_foreign` FOREIGN KEY (`idempresa`) REFERENCES `empresas` (`id`),
ADD CONSTRAINT `locals_idresponsavel_foreign` FOREIGN KEY (`idresponsavel`) REFERENCES `responsavels` (`id`),
ADD CONSTRAINT `locals_idtipolocal_foreign` FOREIGN KEY (`idtipolocal`) REFERENCES `tipolocals` (`id`),
ADD CONSTRAINT `locals_iduf_foreign` FOREIGN KEY (`iduf`) REFERENCES `ufs` (`id`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Script

<?php

require("../conexao.php");

//carga local
    $sql = "SELECT CODIGOLOCAL, EMPRESA, DESCRICAO, CODIGOASCEDENTE, CODIGORESULT, DATAATIVO, RESPONSAVEL, DATADESATIVADO, CODIGOTIPO, TELEFONE, CENTROCUSTO, EMAIL, ENDERECO, COMPLEMENTO, BAIRRO, CIDADE FROM locais";
        echo $sql;
    $res = mysqli_query($con, $sql);
    while ($f = mysqli_fetch_array($res)) {
                echo 'entrei aqui';
                $codigolocal = $f['CODIGOLOCAL'];
                $idempresa = 1;
                $descricaolocal = $f['DESCRICAO'];
                $codigopai = $f['CODIGOASCEDENTE'];
                $result = $f['CODIGORESULT'];
                $dataativo = $f['DATAATIVO'];
                $datadesativado = $f['DATADESATIVADO'];
                $idresponsavel = 1;
                $idtipolocal = $f['CODIGOTIPO'];
                $telefone = $f['TELEFONE'];
                $centrocusto = $f['CENTROCUSTO'];
                $email = $f['EMAIL'];
                $endereco = $f['ENDERECO'];
                $complemento = $f['COMPLEMENTO'];
                $birro = $f['BAIRRO'];
                $cidade = $f['CIDADE'];
                $iduf = 1;
                $created_at = '2017-01-16 22:05:17';
                $updated_at = '2017-01-16 22:05:17';
                echo  $codigolocal;

                $sqluplocal="INSERT INTO localsoriginal (codigolocal, idempresa, descricaolocal, codigopai, result, dataativo,                idresponsavel, idtipolocal, telefone, centrocusto, email, endereco, complemento, birro, cidade, iduf, created_at, updated_at) VALUES ('$codigolocal', '$idempresa', '$descricao', '$codigopai', '$result', '$dataativo', '$idresponsavel', '$idtipolocal', '$telefone', '$centrocusto', '$email', '$endereco', '$complemento', '$birro', '$cidade',
 '$iduf', '$created_at', '$updated_at)";
                echo $sqluplocal;
                $resuplocal = mysqli_query($con, $sqluplocal);
    }
//Fim carga local
echo 'fim';
?>

The mistake you make is relationship

  • 3

    Enter the table structure and script so staff can analyze

  • 1

    You can remove your edit that you placed in the response field... The keys in the editor are for formatting the code...

  • 1

    it is advisable to remove your edits from the answers...

  • Sorry, I don’t understand. Asked to put the structure and the code. What’s wrong?

  • 1

    It is that you put in the answer field and the right is to edit the question even as I did... Now you can go in your answers (which are not answers) and remove them, because you run the risk of taking negatives... You need to apologize No man.... We’re all learning...

  • Got it. I’m looking for where to edit the question to remove.

  • I guess now it’s right.

Show 2 more comments
No answers

Browser other questions tagged

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