0
I’m recording a record in my database like this:
$tipo = $_POST['tipo'];
$data = $_POST['data'];
include 'conexao.php';
$result = mysqli_query($con, "insert into sessoes
(tipo, data) values
('".$tipo."', '".$data."')");
//o registro grava corretamente
if (!$result) { //vejo se tem algum erro
throw new Exception(mysqli_error($con)."insert into sessoes
(tipo, data) values
('".$tipo."', '".$data."')");
}else{
mysqli_commit($con); //dou um commit no banco de dados
$result = mysqli_query($con, "select * sessoes order by id desc");
//NA LINHA ACIMA, TENTO PEGAR O ÚLTIMO REGISTRO SALVO, MAS O MESMO NÃO É RETORNADO, MESMO QUE EU VEJO NO BANCO E ELE ESTÁ LÁ
$linha = mysqli_fetch_assoc($result); //linh fica null
$idSessao = $linha['id']; //idSessao fica null
//header('location:participaram.php?idSessao='.$idSessao);
}
mysqli_close($con);
As you can see in the code, it records the data in the database, but does not return the saved record. What may be wrong?
The table sessoes
:
id int
data date
tipo int
Has the table modeling
sessoes
to pass?– Tiedt Tech
@Marlontiedt changed the question, although the error is not on the table, she is saving normal. just do not return me at the time I need (as soon as saved), if I change page, it appears
– Italo Rodrigo
mysql_fetch_assoc
returns an associative array array. The correct one to access the ID of the returned records is :$linha[0]["id"]
– rdleal
@Panther
$linha
is null– Italo Rodrigo
@Valdeirpsr I do not know this function. what is it for and where the code should use?
– Italo Rodrigo
The function
mysqli_insert_id($con);
serves to return the last ID inserted in the database.– Valdeir Psr
@Valdeirpsr worked perfectly. if you want you can include the answer to earn your points. thanks to others too
– Italo Rodrigo