4
include_once 'acesso_bd/conexao_bd.php';
class PropriedadeDAO {
function inserirPropriedadeBD($propriedade) {
$nome = $propriedade->getNome();
$endereco = $propriedade->getEndereco();
$telefone = $propriedade->getTelefone();
$conexaobd = new ConexaoBD;
$conexao = $conexaobd->conectarAoBD();
$sql = "INSERT INTO propriedade (nome, endereco, telefone) VALUES ('$nome', '$endereco', '$telefone')";
if (!mysqli_query($conexao, $sql)) {
echo "Erro: " . $sql . "<br>" . mysqli_error($conexao);
}
}
the above code refers to the registration of a property, where your ID is AUTO_INCREMENT
.
include_once 'acesso_bd/conexao_bd.php';
class TalhaoDAO {
function inserirTalhaoBD($talhao) {
$nome = $talhao->getNome();
$conexaobd = new ConexaoBD;
$conexao = $conexaobd->conectarAoBD();
$sql = "INSERT INTO talhao (nome, id_propriedade) VALUES ('$nome', LAST_INSERT_ID())";
if (!mysqli_query($conexao, $sql)) {
echo "Erro: " . $sql . "<br>" . mysqli_error($conexao);
}
}
This is the registration code of Talhao, which necessarily needs the foreign key "id_propriedade"
to be identified. I used the LAST_INSERT_ID
, the problem is that is returning me the following error when trying to register Talhao:
Error: INSERT INTO (name, id_property) VALUES ('aaa', LAST_INSERT_ID()) Cannot add or update a Child Row: a Foreign key Constraint fails (
teste
.talhao
, CONSTRAINTid_propriedade_fk
FOREIGN KEY (id_propriedade
) REFERENCESpropriedade
(id_propriedade
) ON DELETE NO ACTION ON UPDATE NO ACTION)
CREATE TABLE IF NOT EXISTS `propriedade` (
`id_propriedade` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(20) NOT NULL,
`endereco` varchar(20) NOT NULL,
`telefone` varchar(20) NOT NULL,
PRIMARY KEY (`id_propriedade`)
) ENGINE=InnoDB ;
CREATE TABLE IF NOT EXISTS `talhao` (
`id_parcela` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(20) NOT NULL,
`id_propriedade` int(11) NOT NULL,
PRIMARY KEY (`id_parcela`),
KEY `id_propriedade` (`id_propriedade`),
KEY `id_propriedade_fk` (`id_propriedade`)
) ENGINE=InnoDB ;
Oops, here at documentation may help you. If I am not mistaken in Mysql you have to put '`' before and after the columns you want to insert values.
– Marconi
Hi, Marconi. Thanks for the comment, but I don’t think the quotes are the problem. I did the test here and it didn’t work.
– Sabrina T.
Sabrina, can [Edit] the question and add the structure of the tables?
– Woss
Added structures.
– Sabrina T.
What is the structure of the tables? Try to change the name of the property table’s Primary key and leave it different from the name of the cut table’s Foreign key. But put the structure of the tables please.
– Diéfani Favareto Piovezan
Now that I’ve seen the structure...it tries to do what I said and puts for example, the property key Primary as
id_prop
And the same names for PK and FK usually give you a headache. .– Diéfani Favareto Piovezan
Diéfani, I had already managed to carry out manual inserts directly in the bank, indicating the exact code of the property at FK and it worked. That is why I believe that the problem really is in LAST_INSERT_ID(). However, I will make a test with your suggestion.
– Sabrina T.
Diéfani, I made a test with your suggestion and you keep making the same mistake.
– Sabrina T.
Sabrina, in table carving the definition of FK would be
CONSTRAINT id_propriedade_fk FOREIGN KEY (id_propriedade) REFERENCES propriedade(id_propriedade)
, because KEY is synonymous with INDEX– Don't Panic