Hola a todos.
Tengo otra tabla en la que me gustaria guardar
el ultimo ID o mejor dicho el ID creado, probe
de esta manera con el anterior ejemplo que esta
funcionando, y creo que no me resulto, espero
me puedan ayudar con este problemilla.
Lo que intente hacer. sin resultado
Código PHP:
Ver original<?php
// Insert query.
$query = 'INSERT INTO client_contact '
. ' (email_addr,target_date,action_step1,action_step2,action_step3)'
. ' VALUES (?,?,?,?,?)';
// Obteniendo el ultimo ID
$dato = "COD-".$ultima;
// Actualizo el campo correspondiente
$query2 = $conexion->query("UPDATE client_contact SET action_step2 = '$dato' WHERE id = '$ultima'");
// EL CAMPO action_step2 ES DONDE se guardara el codigo unico generado, que es un campo de texto.
?>
Lo otro que podria ser es que muestre el ultimo id en un campo de texto
y lo guarde, pero como lo haria si eso pudiera ser una solucion, mas abajo
esta el ejemplo en funcion guarda el id con un UPDATE
Este es el codigo completo
Código PHP:
Ver original<?php
$mode = $_REQUEST['mode'] ? $_REQUEST['mode'] : '';
// Print the form.
if (!$mode) {
?>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() { $("#datepicker").datepicker() });
</script>
<style>
label { display:inline-block; width:100px }
</style>
</head>
<body>
<h1>Enter Your Action Items</h1>
<form method="post" action="form.php">
<div>
<label for="email_addr">Email Address:</label>
<input name="email_addr" type="text" maxlength="128" size="64"/>
</div>
<div>
<label for="target_date">Target Date:</label>
<input name="target_date" type="text" id="datepicker"/>
</div>
<div>
<label for="action_step1">Action Step 1:</label>
<input name="action_step1" type="text" size="64"/>
</div>
<div>
<label for="action_step2">Action Step 2:</label>
<input name="action_step2" type="text" size="64"/>
</div>
<div>
<label for="action_step3">Action Step 3:</label>
<input name="action_step3" type="text" size="64"/>
</div>
<input type="hidden" name="mode" value="submit_form"/>
<input type="submit" value="Submit"/>
</form>
</head>
</html>
<?php
}
// Process the form input.
elseif ($mode == 'submit_form') {
// Form data.
$email_addr = $_REQUEST['email_addr'];
$target_date = $_REQUEST['target_date'];
$step1 = $_REQUEST['action_step1'];
$step2 = $_REQUEST['action_step2'];
$step3 = $_REQUEST['action_step3'];
// Convert target date into Y-m-d format:
// Insert query.
$query = 'INSERT INTO client_contact '
. ' (email_addr,target_date,action_step1,action_step2,action_step3)'
. ' VALUES (?,?,?,?,?)';
// Connect and execute.
$link = new mysqli('localhost', 'contactor', '987654321', 'test');
$stmt = $link->prepare($query);
$stmt->bind_param('sssss', $email_addr, $target_date, $step1, $step2, $step3);
$res = $stmt->execute();
// Show the user if it worked or not.
if ($res) {
echo "Success! You have successfully entered your information!";
}
else {
echo "Oops, something went wrong!";
}
// WUT? (This is for debugging; get rid of this, for production.)
if (!$res) {
echo "INSERT FAILED:\n\n";
}
}
?>
Primera Tabla Que funciona perfecto
guarda el Codigo Unico mas el ID
Resultado en el campo de la tabla es:
COD-1
COD-2
COD-3
etc...
Nota: Los numeros son los ID
del autoincrement de la tabla (Personas)
Código PHP:
Ver original<?
include_once('conectar.php'); // Conexion
$conexion = mysqli_connect($dbhost,$dbuser,$dbpass,$dbdatabase) or
die ('error'); // Verificar Conexión if (isset($_POST['action']) && $_POST['action'] == 'add') {
if (isset($_GET['idp'])) $idp = $_GET['idp']; // ID de Personas $Nombre = $_POST['Nombre'];
$Apellido = $_POST['Apellido'];
$Anio = $_POST['Anio'];
$CodigoUnico = $_POST['CodigoUnico']; // AGREGE POR SI FALTA Y NADA
//realizo la insercion de los datos pero dejando en blanco el campo CodigoUnico para su posterior actualizacion
$inserta = $db->query("INSERT INTO Personas (Nombre,Apellido,Anio,CodigoUnico) VALUES ('$Nombre','$Apellido','$Anio','')");
//tomo la ultima id
//creo el codigo de esta
$dato = "COD-".$ultima;
//actualizo el campo correspondiente
$actu = $db->query("UPDATE Personas SET CodigoUnico = '$dato' WHERE id = '$ultima'");
if($actu){
echo "Listo";
echo "ID es: " . $ultima; // AQUI SOLO ME MUESTRA EL ID INGRESADO AL DB
} else {
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<form action="tes.php" method="post">
<input type="hidden" name="action" value="add">
<input type='hidden' name='submit' value='submit'>
<input name="Nombre" type="text">
<input name="Apellido" type="text">
<input name="Anio" type="text">
<input name="enviar" type="submit">
</form>
</body>
</html>