Ver Mensaje Individual
  #9 (permalink)  
Antiguo 22/11/2015, 13:16
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
Respuesta: mysqli_insert_id en un campo de texto

Cita:
Iniciado por pateketrueke Ver Mensaje
¿Y cómo es posible que si te imprima dicho ID antes del formulario y después ya no?

¿Podrías mostrar el código completo como lo tienes ahora?

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