Get a record as soon as it is saved in the bank

Asked

Viewed 89 times

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
  • 1

    Has the table modeling sessoes to pass?

  • @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

  • 1

    mysql_fetch_assoc returns an associative array array. The correct one to access the ID of the returned records is : $linha[0]["id"]

  • @Panther $linha is null

  • @Valdeirpsr I do not know this function. what is it for and where the code should use?

  • 1

    The function mysqli_insert_id($con); serves to return the last ID inserted in the database.

  • @Valdeirpsr worked perfectly. if you want you can include the answer to earn your points. thanks to others too

Show 2 more comments

1 answer

1


The mistake

The mistake is in query. Missed the FROM. The correct is: select * FROM sessoes order by id desc

Below explains why you should not use this way to capture the last ID.

Solutions

Utilize mysqli_insert_id to return the last inserted ID.

  $idSessao = mysqli_insert_id($con);
  header('location:participaram.php?idSessao='.$idSessao);

In addition, you can also use the query:

SELECT LAST_INSERT_ID() AS lastId;

It’s the same thing, but a little more code (due to PHP).

Remarks

There is a problem in using select * sessoes order by id desc. When it comes to performance, this code is not recommended. Maybe at the beginning and on localhost, seems interesting, the problem is that on a production server with thousands of access it becomes a nightmare, especially when you have a lot of data inserted.

Before using a code Mysql use the EXPLAIN in front, that way you’ll be able to see how query behaves when executed.

Ex: EXPLAIN SELECT * FROM sessoes ORDER BY id desc

Browser other questions tagged

You are not signed in. Login or sign up in order to post.