fabao1966 Posted May 18, 2014 at 07:21 PM Report #556174 Posted May 18, 2014 at 07:21 PM 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> -->
HappyHippyHippo Posted May 19, 2014 at 07:56 AM Report #556212 Posted May 19, 2014 at 07:56 AM $pessoas = "insert into pessoas (nome_pessoa) values('$nome')"; mysql_query ($pessoas, $conexao) or die (mysql_error()); $id = mysql_insert_id(); $inserir = "insert into enderecos (pessoa_id, logradouro) values('$id', '$endereco'"; IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now