-1
How do I get the last one ID
created by insert
?
$rs = $mysqli->prepare("INSERT INTO tabela (campo1, campo2, campo3) VALUES (?, ?, ?) ");
$rs->bind_param('sss',
$_POST['campo1'],
$_POST['campo2'],
$_POST['campo3']
);
$rs->execute();
if ($rs->errno) {
echo 'Erro: ', $rs->error;
} else {
echo "<script>window.location='PAGINA.php'</script>";
}
Just use $ultimoId= $rs->insert_id;
?
Then I can do it like this?
$rs->execute();
$ultimoId = $rs->insert_id;
if ($rs->errno) {
echo 'Erro: ', $rs->error;
} else {
echo "<script>window.location='PAGINA.php?id=$ultimoId'</script>";
}
the $rs variable is your connection object? post your complete code so that we can understand right what are the variables
– Julyano Felipe
https://answall.com/questions/127456/forma-otimizada-recuperar-ultimo-id-mysql
– Pedro Henrique
@Julyanofelipe,
$mysqli
is the variable that has the database data.$rs
is what receives the Insert and executes.– Tiago
@James then what you’re looking for would be
$conn->insert_id
;– Julyano Felipe
just use
$mysql->insert_id
– Kayo Bruno