I have 2 tables for example a table "users" containing a primary key ID
user and another table "details" with a foreign key do usuario.ID
My need would be through php to do for example in a single query the user's INSERT and its details. Currently I was able to do this as follows:
$sql = "INSERT INTO usuario (usuario.nome,usuario.cpf) VALUES ('$nome','$cpf')";
$trans1 = $mysqli->query($sql);
$sql = "INSERT INTO detalhes (usuario.user_id,detalhes.civil,detalhes.idade) VALUES ($mysqli->insert_id,'$civil','$idade')";
$trans2 = $mysqli->query($sql);
if ($trans1 AND $trans2 == 1){
echo "<script type='text/javascript'>alert('Usuário Cadastrado');document.location.href=\"index.php?pagina=home\"</script>";
} else {
echo "<script type='text/javascript'>alert('Não foi possível inserir o usuario.');document.location.href=\"index.php?pagina=home\"</script>";
}
$conn->close();
I do not know if this form is correct, but that's what I got for the moment.
I spent hours trying to get the same result through multi_query () but I'm not getting it at all.
Does anyone have any light?
Thanks!