Primary key transfer to foreign key - PHP

Asked

Viewed 63 times

0

I am having a problem entering data into my database. I have two connected tables, company and information.

STRUCTURE OF THE COMPANY TABLE

CREATE TABLE IF NOT EXISTS `empresa` (
  `empresa_id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(30) NOT NULL,
  `senha` varchar(8) NOT NULL,
  `cnpj` varchar(14) NOT NULL,
  `telefone` varchar(45) NOT NULL,
  `nome` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`empresa_id`),
  UNIQUE KEY `email_UNIQUE` (`email`),
  UNIQUE KEY `cnpj_UNIQUE` (`cnpj`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

STRUCTURE OF THE INFORMATION TABLE

CREATE TABLE IF NOT EXISTS `informacoes` (
  `informacoes_id` int(11) NOT NULL AUTO_INCREMENT,
  `nome_fantasia` varchar(20) NOT NULL,
  `endereco` varchar(45) NOT NULL,
  `descricao_da_empresa` varchar(140) NOT NULL,
  `inicio_expediente` time NOT NULL,
  `fim_expediente` time NOT NULL,
  `tipo_estabelecimento` varchar(11) NOT NULL,
  `foto_perfil` varchar(20) NOT NULL,
  `empresa_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`informacoes_id`),
  KEY `fk_informacoes_idx` (`empresa_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8

As you can see, both are connected via the "empresa_id" foreign key. Okay, my bank is working perfectly, but I’m encountering trouble linking foreign and primary keys.

METHOD SAVE COMPANY

public function salvar($empresa){

          try{
            $sql = "INSERT INTO empresa(email, senha, cnpj) VALUES (?,?,?)";
            $stmt = $this->pdo->prepare($sql);
            $stmt->bindValue(1, $empresa->getEmail());
            $stmt->bindValue(2, $empresa->getSenha());
            $stmt->bindValue(3, $empresa->getCNPJ());
            $stmt->execute();
            if(isset($stmt)){
              echo "<script>alert('Cadastro quase finalizado');window.location.href='../informacoes.html'</script>";
            }
          }catch(PDOException $e){
            echo $e->getMessage();
          }

}



METODO SALVAR INFORMACOES

public function inserirInformacoes($dados, $name, $tmp, $size){

            $ext = end(explode('.', $name));

            $pasta = '../imagens-perfil';
            $maxSize = '1024 * 1024 *2';
            $permitir = array('jpg', 'jpeg', 'png');

            $name = uniqid().'.'.$ext;


            $sql = "INSERT INTO informacoes(nome_fantasia, endereco, descricao_da_empresa, inicio_expediente, fim_expediente, tipo_estabelecimento, foto_perfil, empresa_id) VALUES (?,?,?,?,?,?,?,?)";


            $stmt = $this->pdo->prepare($sql);
            $stmt -> bindValue(1, $dados->getNome());
            $stmt -> bindValue(2, $dados->getEndereco());
            $stmt -> bindValue(3, $dados->getDescricao());
            $stmt -> bindValue(4, $dados->getInicioExpediente());
            $stmt -> bindValue(5, $dados->getFimExpediente());
            $stmt -> bindValue(6, $dados->getTipoEstabelecimento());
            $stmt -> bindValue(7, $name);
            $stmt -> bindValue(8, );
            $stmt -> execute();

            if(isset($stmt)){
                $upload = move_uploaded_file($tmp, $pasta.'/'.$name);
                if($upload){
                    echo "<script>alert('Postado com sucesso');window.location.href='../index.html'</script>";
                }
            }

}

As I said above, my bank is working perfectly, however I can’t recover the id created on entering the data in the table company and throw it to the table information as a foreign key. Remembering that the two methods are in separate files.

Thanks for your help.

  • https://answall.com/q/174010/5878

  • It shouldn’t be: Foreign Key fk_informacoes_idx (empresa_id) REFERENCES empresa (empresa_id)?

  • Yes it is true, I forgot to edit this before posting the question. Thank you for reminding me :)

No answers

Browser other questions tagged

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