Forum Moderators: coopster
But it donīt work, maybe the id been lost after doing an last_insert_id.
I have an form to insert bookings, and on this after I inserted to one table, I also insert last_insert_id to another, then if they say yes (si) in one output they are refreshed to another page to complete another form but I donīt know how to pass the id.
This is my code:
$query = "insert into bookings (llegada, tipo, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, limpieza, transporte, comentarios)" .
"values ('$llegada', '$tipo', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$limpieza', '$transporte', '$comentarios')";
mysql_query($query);
$query = "INSERT INTO propiedades (id_reserva, propiedad)" .
"VALUES (LAST_INSERT_ID(), '$propiedad')";
mysql_query($query);
if (isset($limpieza)) {
if ($limpieza =="si") {
echo "<meta http-equiv=\"refresh\" content=\"1;URL=insertar_limpiezas.php?id=$_GET['id']\">";
thanks in advance
Furthermore, don't you need to use:
echo "<meta http-equiv=\"refresh\" content=\"1;URL=insertar_limpiezas.php?PHPSESSID=$_GET['id']\">";
instead of your version (unless you've changed the default session name [be2.php.net...] Or are you trying to set it with session_id()?
I tried with mysql_insert_id.
Is there a way to insert in one table, insert the same id to another and finally get that id and pass it with an redirect?
if (isset($limpieza)) {
if ($limpieza=="no") {
header ("Location: reservas.php");
}
elseif ($limpieza=="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["id"]."");
}
mysql_query($query);
$lastid = mysql_query("SELECT LAST_INSERT_ID()", $dbh);
echo '<br>'.$lastid['id'].'<br>'; //comment out these line two lines or remove after first debug check
die();
if (isset($limpieza)) {
if ($limpieza=="no") {
header ("Location: reservas.php");
}
elseif ($limpieza=="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["id"]."");
}
mysql_query($query);
$lastid = mysql_query("SELECT LAST_INSERT_ID()", $dbh);
if you get $lastid['id'] with the value it needs to be, delete the first two lines - and then in your script insertar_limpiezas.php, add somewhere:
echo $_SERVER['REQUEST_URI'];
remember, this value will be available there as $_GET['id'];
this is the code, on top:
if (isset($limpieza)) {
if ($limpieza=="no") {
header ("Location: reservas.php");
}
elseif ($limpieza=="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["id"]."");
}
else
{
echo "<h1>Comprueba fechas, horas y campos obligatorios</h1>";
echo "<input type=\"button\" value=\"Go back\" onclick=\"history.back();\">";
}
}
if ($_POST['enviar']) {
// process form
include("conex/conexion.php");
$query = "insert into bookings (llegada, tipo, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, limpieza, transporte, comentarios)" .
"values ('$llegada', '$tipo', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$limpieza', '$transporte', '$comentarios')";
mysql_query($query);
$query = "INSERT INTO propiedades (id_reserva, propiedad)" .
"VALUES (LAST_INSERT_ID(), '$propiedad')";
mysql_query($query);
$lastid = mysql_query("SELECT LAST_INSERT_ID()", $dbh);
}
$query = "insert into bookings (llegada, tipo, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, limpieza, transporte, comentarios)" .
"values ('$llegada', '$tipo', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$limpieza', '$transporte', '$comentarios')";
mysql_query($query);
$query = "INSERT INTO propiedades (id_reserva, propiedad)" .
"VALUES (LAST_INSERT_ID(), '$propiedad')";
mysql_query($query);
$lastid = mysql_query("SELECT LAST_INSERT_ID()", $dbh);
if (isset($limpieza)) {
if ($limpieza=="no") {
header ("Location: reservas.php");
}
elseif ($limpieza=="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["id"]."");
}
else
{
echo "<h1>Comprueba fechas, horas y campos obligatorios</h1>";
echo "<input type=\"button\" value=\"Go back\" onclick=\"history.back();\">";
}
}
?>
I also tried to put the select last_insert_id after the first insert and the same
$lastid = mysql_query("SELECT LAST_INSERT_ID() AS myid", $dbh);
if (isset($limpieza)) {
if ($limpieza=="no") {
header ("Location: reservas.php");
}
elseif ($limpieza=="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["myid"]."");
}
else
...
Using this I get an id in the url, but always the same id=S so something is wrong.
$query = "insert into bookings (id, llegada, tipo, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, limpieza, transporte, comentarios)" .
"values ('$id', '$llegada', '$tipo', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$limpieza', '$transporte', '$comentarios')";
$ult_id = ("SELECT mysql_insert_id($dbh)");
mysql_query($query);
$query = "INSERT INTO propiedades (id, propiedad)" .
"VALUES (LAST_INSERT_ID(), '$propiedad')";
mysql_query($query);
if (isset($limpieza)) {
if ($limpieza =="si") {
header ("Location: insertar_limpiezas.php?id=".$ult_id["id"]."");
}
else {
header ("Location: reservas.php");
}
}
}
else
{
echo "<h1>Comprueba fechas, horas y campos obligatorios</h1>";
echo "<input type=\"button\" value=\"Go back\" onclick=\"history.back();\">";
}
}
Then adding the code in the link above for error reporting, I got these errors:
Notice: Undefined index: ult_id in ...bla \insertar_reserva3.php on line 28
Notice: Undefined variable: id in ...bla\insertar_reserva3.php on line 58
Warning: Cannot modify header information - headers already sent by (output started at bla\insertar_reserva3.php:24) in ...bla\insertar_reserva3.php on line 68
I just donīt know whatīs wrong.
the headers works, just donīt put the id in the url.
If I change: header ("Location: insertar_limpiezas.php?id=".$ult_id["id"]."");
to: echo "$ult_id"; I get the first two errors and as well: SELECT last_insert_id, (Resource id #3) and that id is not correct.
$query = "insert into bookings (id, llegada, tipo, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, limpieza, transporte, comentarios)" .
"values ('$id', '$llegada', '$tipo', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$limpieza', '$transporte', '$comentarios')";
mysql_query($query);
// You may want to do some edit checking
// here to make sure query processed successfully
// before continuing...
$query = "INSERT INTO propiedades (id, propiedad)" .
"VALUES (LAST_INSERT_ID(), '$propiedad')";
mysql_query($query);
$query = mysql_query("SELECT LAST_INSERT_ID() AS myid", $dbh);
$lastid = mysql_fetch_array($query);
if (isset($limpieza)) {
if ($limpieza =="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["myid"]."");
}
else {
header ("Location: reservas.php");
}
}
}
else
{
echo "<h1>Comprueba fechas, horas y campos obligatorios</h1>";
echo "<input type=\"button\" value=\"Go back\" onclick=\"history.back();\">";
}
}