Receive POST ID and play to Location via GET

1

I'm looking forward to getting the ID of the entity that just registered. Only it will be played to another screen, to be registered other information. I wonder how I can get the entity ID when saving the form.

This is HTML

<form action="set.php" method="post">

	 <div class="col-md-6">
		 <div class="form-group">
			<label class="control-label">*Nome</label>
			<input type="text" id="nome" name="nome" class="form-control" placeholder="">
		 </div>
     </div>
     <div class="col-md-6">
		<div class="form-group">
			<label class="control-label">Sobrenome</label>
			<input type="text" id="sobrenome" name="sobrenome" class="form-control" placeholder="">
		</div>
	 </div>

And this is PHP

<?php

$nome                = $_POST ["nome"];  
$sobrenome           = $_POST ["sobrenome"]; 

$sql = mysql_query ("INSERT INTO gr_entidade (nome, sobrenome ) VALUES ('$nome', '$sobrenome')");

header("Location: usuario?id=id");
?>

header("Location: usuario?id=id");

Can you give me a hand?

    
asked by anonymous 24.12.2017 / 21:56

2 answers

1

According to PHP documentation :

Use: mysql_insert_id() .

Ex:

<?php

$nome                = $_POST ["nome"];  
$sobrenome           = $_POST ["sobrenome"]; 

$sql = mysql_query ("INSERT INTO gr_entidade (nome, sobrenome ) VALUES ('$nome', '$sobrenome')");

$last_id = mysql_insert_id();

header("Location: usuario?id=$last_id");
?>

Note that the mysql extension is obsolete, according to the same documentation:

  

Warning This extension has been deprecated since PHP 5.5.0 and has been removed in   PHP 7.0.0. Use MySQLi or PDO_MySQL alternatively. See too   MySQL: Choosing an API and related FAQs for more information.   Alternatives to this function include:

     

mysqli_insert_id ()

     

PDO :: lastInsertId ()

We recommend that you migrate to the mysqli or PDO extensions.

    
24.12.2017 / 22:19
1

To capture the last added ID, simply run the sql query.

mysql_query("SELECT LAST_INSERT_ID()");

Or you can use the mysql_insert_id     

24.12.2017 / 22:04