2
I have 2 tables for example a table "users" containing a primary key ID
user and other table "details" with a foreign key do usuario.ID
My need would be for php to do for example in a single query o INSERT user and their details. Currently I managed to do this in the following way:
$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 don’t know if this shape is right, but it’s the one I got for now.
I spent hours trying to get the same result through multi_query() but I’m not getting it at all.
Someone has some light?
Thanks!
You can do a little different using transactions.
– rray