Ver Mensaje Individual
  #15 (permalink)  
Antiguo 22/11/2015, 14:11
Avatar de gonzaherrera77
gonzaherrera77
 
Fecha de Ingreso: noviembre-2015
Mensajes: 60
Antigüedad: 8 años, 5 meses
Puntos: 7
Respuesta: mysqli_insert_id en un campo de texto

Hola buenas tardes, proba si funciona asi

Código PHP:
Ver original
  1. <?php
  2.  include_once('conectar.php'); // Conexion
  3.  $conexion=mysqli_connect("localhost","user","password","db");
  4.  
  5. if(isset($_REQUEST['enviar'])) {
  6.     if (mysqli_connect_errno()) {
  7.        echo "Error MySQL: " . mysqli_connect_error();
  8.     }
  9.  
  10.     if (isset($_GET['idp'])) {
  11.        $idp = $_GET['idp']; // ID de Personas
  12.        $Nombre = $_POST['Nombre'];
  13.        $Apellido = $_POST['Apellido'];
  14.        $Anio = $_POST['Anio'];
  15.        $CodigoUnico = $_POST['CodigoUnico'];
  16.        $SQL = "INSERT INTO Personas (Nombre,Apellido,Anio,CodigoUnico) VALUES ('$Nombre','$Apellido','$Anio','$CodigoUnico')";
  17.     }  
  18.  
  19.     if (mysqli_query($conexion,$SQL)) {
  20.        echo "Listo";
  21.        // Print auto-generated id
  22.        //echo " " . mysqli_insert_id($conexion);
  23.       $last_id = mysqli_insert_id($conexion);
  24.       echo "ID es: " . $last_id; // AQUI SOLO ME MUESTRA EL ID INGRESADO AL DB
  25.     }
  26.     else {
  27.        echo "Error: " . $SQL . "<br>" . mysqli_error($conexion);
  28.    }
  29. }
  30. ?>
  31.  <!DOCTYPE html>
  32.  <html>
  33.     <head>
  34.        <meta charset="utf-8">
  35.        <title>Test</title>
  36.     </head>
  37.     <body>
  38.       <form action="tes.php" method="post">
  39.          <input name="Nombre" type="text">
  40.          <input name="Apellido" type="text">
  41.          <input name="Anio" type="text">
  42.          <input name="CodigoUnico" type="text" value="<?php $last_id = (isset($last_id)) ? $last_id : ""; echo $last_id;?>">
  43.          <input name="enviar" type="submit">
  44.       </form>
  45.     </body>
  46.  </html>

Te explico primero entras al archivo test.php cargas el formulario, una vez cargado el formulario en el input codigoUnico te tendria que paracer dicho id.

¡Saludos!