Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá pessoal, estou tendo um problema ao inserir dados no meu banco. Tenho duas tabelas conectadas, empresa e informacões.
ESTRUTURA DA TABELA EMPRESA
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 ;
ESTRUTURA DA TABELA INFORMACOES
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
como podem ver, ambas estão ligadas através da chave estrangeira "empresa_id". Okay, meu banco está funcionando perfeitamente, porém estou encontrando problema ao vincular as chaves estrangeiras e primarias.
METODO SALVAR EMPRESA
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>";
}
}
}
Como disse mais acima, meu banco está funcionando perfeitamente, entretanto não consigo recuperar o id criado na inserção dos dados na tabela empresa e joga-lo para a tabela informações como uma chave estrangeira. Lembrando que os dois métodos estão em arquivos separados.
Desde já agradeço a ajuda de todos.Carregando comentários...