Forum Moderators: coopster

Message Too Old, No Replies

pass id meta refresh

         

helenp

11:43 am on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
I want to pass the id doing an metarefresh,
I found in php.net that this is the way to do it for seasons:
Session ID's wont be carried over through meta refreshes, so make sure you add the variables (in GET format) to the URL.
IE )
<meta http-equiv=\"refresh\" content=\"1;URL=index.php?PHPSESSID=$PHPSESSID\">

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

mincklerstraat

4:08 pm on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



where's $_GET['id'] coming from? The url the user is coming from, in the form of whatever.php?id=456? Or a form that uses the get method and has a field with name="id"? If not, there might not be any variable available called $_GET['id']. This is my first thought if you're having problems here, for whatever it's worth. I avoid meta refreshes, so I don't have experiences with them and sessions.

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()?

helenp

5:05 pm on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks,
but I am not doing sessions that is an exampel in php,
actually I need to pass the id from the first insert, that is the same id that I insert in the second insert with last_insert_id. Until here it works.
The problem is really I donīt have the id I understand now, I am new to this....

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?

coopster

5:41 pm on Sep 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure.
  1. Run your first INSERT query.
  2. Run your second INSERT query.
  3. SELECT the LAST_INSERT_ID() and store it in a variable in your script.
  4. Use PHP's header() [php.net] function to send the user to the new page.

helenp

9:07 am on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks,
I been hours to do it, finally everything works, the first redireccion works perfect, but canīt find out how to pass the variable, tried everything, with and without ' etcetera.
The variable works if I put it in an echo, so the problem is how to pass it to have it in the url.

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);

mincklerstraat

10:36 am on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Time for 'debugging 101', then, to see what's going wrong. First though a little nitpicking. You said you tried a variety of quotes options. Well, replace this line in your code:
header ("Location: insertar_limpiezas.php?id=".$lastid["id"]."");
with this for starters (above last set of quotes redundant, and it's easier to work with double quotes in a string if you use single quotes in your php):
header ('Location: insertar_limpiezas.php?id='.$lastid['id']);
You mention already having tried 'echo', we'll do this again just for sure:

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'];

helenp

11:03 am on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



nop, this echo died........
so that means I donīt have the id.
On my pc I need to use the $_GET etc, but on the host there is no need, checked both, so thats not the error.

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);
}

coopster

12:30 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Check message number 4 again, note the order. You must INSERT your row(s) first, then get the brand new ID, then you can redirect. Otherwise your id variable is not going to have any data in it yet :)

helenp

12:57 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks a lot to all, but as I see it the order is the same...
Now I tried to put all php.script before the html tags and the same ....
<?php
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);
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

coopster

1:49 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your header() [php.net] function is referencing an associative index in the result set that does not exist. If you had error_reporting() [php.net] turned on, you will catch these types of issues quicker. I often use an ALIAS when using LAST_INSERT_ID(). Note the bold change:
$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
...

helenp

1:16 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



nothing, think I will throw my hair.

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.

coopster

3:57 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was merely showing you the correct syntax, that was only partial code to emphasize the processing order and statement syntax. You need to get your order correct and use the correct resource identifiers. Also, the data is not available until you fetch the row from the result set. I did not test this, but it should give you a better understanding of the steps required and the processing order...

$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();\">";
}
}

helenp

4:20 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



oh my good,
thats it, mysql_fetch_array was the missing bit.
everything works just perfect.
I thought that was only to use when showing several results.
Thanks a lot.