Jump to content

Inserir em duas tabelas pelo mesmo form com chave estrangeira


Recommended Posts

Posted

esou tentando inserir em duas tabelas, elas tem ligação de chave estrangeira, como inserir para pegar a chave primária da primeira para inserir na segunda? segue o código:

<?php
   include("conexao.php");

   $nome = $_POST['nome'];
   $endereco = $_POST['endereco'];


   $pessoas = "insert into pessoas (nome_pessoa)
    values('$nome')";
   $consulta = mysql_query ($pessoas, $conexao)
	    or die (mysql_error());

   #$id = mysql_insert_id();    
   $sql="SELECT last_insert_id(id) from pessoas";
   $resultado = mysql_query($sql, $conexao)
			 or die (mysql_error());

   #$id = "SELECT MAX(id) FROM pessoas";


   $inserir = "insert into enderecos (pessoa_id, logradouro)
    values('$resultado', '$endereco'";

    $consulta2 = mysql_query ($inserir, $conexao)
	    or die (mysql_error());  

    if ($consulta) {
	    echo "inserção realisada";
    }

    echo "<meta http-equiv='refresh' content='5 url=index.html'>";
    include("desconecta.php");	   
?>

<!--<script type="text/javascript">
   location.href="http://localhost/agenda/index.php";
</script> -->

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.