Insert value in table and automatically obtain the registration ID that you will have

0

I am making an insertion to a database and would like that automatically after the INSERT the PHP file will get the ID that corresponds to the record (ID_PLANILLA).

This I need since I must automatically show a graph with the data obtained.

EH thought of something like getting the last ID in the database and adding 1, but I do not know how to do it.

PHP insert:

  $stmt = $conexion->prepare("INSERT INTO sanciones (
          unidad,
          cedula_sancionado,
          cedula_sancionador, 
          cedula_superior,
          articulo_falta, 
          aparte_falta, 
          documento_seleccion,
          fecha_inicio, 
          fecha_termino, 
          dias_sancion, 
          aclaratoria_sancion, 
          estado_sancion_id,
          medida_id,
          articulo_circunstancias,
          agravante_seleccion, 
          atenuante_seleccion, 
          total_medida,
          total_demerito_final, 
          estado_lugar_id) 
          VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

    $stmt->bind_param("siiiiisssisiiissddi", 
      $unidad,
      $cedula1, 
      $cedula2, 
      $cedula3, 
      $articulo_falta, 
      $aparte_falta, 
      $documentos_implode, 
      $fecha_inicio, 
      $fecha_termino, 
      $dias, 
      $aclaratoria, 
      $estado_sancion_id, 
      $tipo_medida, 
      $tipo_circunstancia, 
      $agravantes_implode, 
      $atenuantes_implode, 
      $total_medida, 
      $total_demerito_final, 
      $lugar);
      $stmt->execute();
    if ($stmt)
      {
$idplanilla= lastid
      echo ('<script>alert("Datos insertados satisfactoriamente!");</script>');
      echo ("<script>window.location = 'grafica_unidades.php?idplanilla=<?=$idplanillla?>"
      $stmt->close();
      }
      else
      {
      echo ('<script>alert("No se pudo generar la OMD");</script>');
      echo ("<script>window.location = 'menu_generar_omd_unidades.php';</script>");
      }
    
asked by Victor Alvarado 24.04.2017 в 19:20
source

1 answer

5

Class mysqli offers a property called insert_id .

  

The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query does not update a value AUTO_INCREMENT .

     

Note: If the number is greater than the maximum value int, mysqli_insert_id() will return a string.

It is also worth noting that the class mysqli_stmt also offers the same property called insert_id , but:

  

It should be noted that the use of mysqli_stmt->insert_id will not result in the return of a unique ID for each execution of a prepared insert instruction. In practice, it seems that the first insertion ID is returned. If you are making multiple inserts with the same prepared statement (one invocation of mysqli_stmt::prepare and several invocations of mysqli_stmt::execute() for a given statement), and you need to keep the unique ID for each insert, use mysqli_connection->insert_id .

Applied to your code would be like this:

//...
$total_demerito_final, 
$lugar);

if ($stmt->execute()) { // <-- AQUI tenias un error
  $idplanilla = $stmt->insert_id; // <-- AQUI obtenemos el ID
  //...

NOTE: The statement if($stmt) will always be truly , that's why you should rewrite it as I did in my code.

    
answered by 24.04.2017 / 20:16
source