Erro ao cadastrar tabela associativa

0 respostas
R
...
$sql = "INSERT INTO cliente (nmcliente, dtnascimento, dcendereco, enestadocivil, ensexo, txcomentario, dtcadastro)
                        VALUES ('$_POST[txt_nome]','" . dataDeNasc() . "','$_POST[txt_endereco]','$_POST[slt_civil]','$_POST[rdo_sexo]','$_POST[txa_comentario]','" . dataDeCadastro() . "')";
                if (!mysql_query($sql, $con)) {
                    die('Error Cliente: ' . mysql_error());
                }
                if (isset($_POST["chk_hobbies"])) {
                    for ($i = 0; $i < count($_POST["chk_hobbies"]); $i++) {

                        $nome = $_POST["chk_hobbies"][$i];

                        $sql2 = "INSERT INTO hoobies (nmhoobies) 
                                VALUES ('$nome')";

                        if (!mysql_query($sql2, $con)) {
                            die('Error Hobbies: ' . mysql_error());
                        }
                    }
                    $sql3 = "INSERT INTO hoobies_cliente (idhoobies,idcliente)
                                VALUES ('test.hoobies.idhoobies','test.idcliente.idcliente')";
                    if (!mysql_query($sql3, $con)) {
                        die('Error hobbies_cliente: ' . mysql_error());
                    }
                }
Error hobbies_cliente: Cannot add or update a child row: a foreign key constraint fails (`test`.`hoobies_cliente`, CONSTRAINT `fk_hoobies_has_cliente_hoobies1` FOREIGN KEY (`idhoobies`) REFERENCES `hoobies` (`idhoobies`) ON DELETE NO ACTION ON UPDATE NO ACTION)

Dados estão na tabela cliente e hobbies estão populados.

Gostaria que fosse feito isso:
test.hoobies_cliente.idhobbies <= test.hoobies.idhoobies
test.hoobies_cliente.idcliente <= test.cliente.idcliente

Alguma ajuda????

Criado 27 de maio de 2012
Respostas 0
Participantes 1