Forum Moderators: coopster

Message Too Old, No Replies

last insert id no longer works

         

helenp

11:35 am on Nov 13, 2005 (gmt 0)

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



Help,
I donīt know what happened, if server changed something,
as I know I havnīt changed anything,

everything works when I insert last inserted id until the code that is in bold, it did work before, now instead of insert the last inserted id, it just pick anyone?

$query = "insert into bookings (id, llegada, tipo, tiporeserva, salida, tipo_sal, hora_llegada, hora_salida, propiedad, cliente, reservado, comentarios)" .
"values ('$id', '$llegada', '$tipo', '$tiporeserva', '$salida', '$tipo_sal', '$hora_llegada', '$hora_salida', '$propiedad', '$cliente', '$reservado', '$comentarios')";
mysql_query($query);

$query = mysql_query("SELECT LAST_INSERT_ID() AS myid", $dbh);
$lastid = mysql_fetch_array($query);

if (isset($pack)) {
if ($pack =="si") {
$query = "INSERT INTO pack (id_reserva, pack, tipopack, fechapack)" .
"VALUES (LAST_INSERT_ID(), '$pack', '$tipopack', '$llegada')";
mysql_query($query);
}
}
$query = "INSERT INTO pagados (id_reserva, forma_pago, porcentaje)" .
"VALUES (LAST_INSERT_ID(), '$forma_pago', '$porcentaje')";
mysql_query($query);

$result = mysql_query ("SELECT
(CASE WHEN ('$llegada' BETWEEN nov_inicio AND nov_fin and '$salida' BETWEEN nov_inicio AND nov_fin)
THEN sum(novnet) * (TO_DAYS('$salida') - TO_DAYS

etc (very long code)
FROM precios where precios.id_propiedad = '$propiedad'
", $dbh);
$row = mysql_fetch_array($result); {
$price=$row["price"];
$price1=$row["price1"];
$price2=$row["price2"];
etc (very long code)
}
$total=$price+$price1 etc (very long code)
$half_round = round(($total*2), 0)/2;

$query = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES (LAST_INSERT_ID(), '$llegada', '$propiedad', '$half_round')";
mysql_query($query);

if (isset($limpieza)) {
if ($limpieza =="si") {
header ("Location: insertar_limpiezas.php?id=".$lastid["myid"]."");
}
else {
header ("Location: reserva_hecha.php?id=".$lastid["myid"]."");
}

}

Please help I am desperate,
why doesnīt LAST_INSERT_ID(), work in the bold query?
everything else works perfect.

Thanks in advance

helenp

12:07 pm on Nov 13, 2005 (gmt 0)

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



He probado usar '$lastid' y '$myid'
y no funciona tampoco

helenp

12:46 pm on Nov 13, 2005 (gmt 0)

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



I donīt understand anything.

I tried with an absolute value and do not work
$query1 = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES (500, '$llegada', '$propiedad', '$half_round')";
mysql_query($query1);
As well with:
$query1 = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES ('500', '$llegada', '$propiedad', '$half_round')";
mysql_query($query1);
None works from the page

However this works perfect in mphpmyadmin:
INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)
VALUES (500, '2005-11-13', 'Villa_Los_Naranjos', '500')

Seems like the problem is in the php way of doing it,
but it is the same as previous inserts?

helenp

1:20 pm on Nov 13, 2005 (gmt 0)

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



This on an new page works perfect as well

if ($_REQUEST['enviar'])
{

$query1 = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES (500, '2005-11-13', 'Villa_Los_naranjos', '500')";
mysql_query($query1);
}

Where in ....pi.....is the error?

RonPK

1:21 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What errors are reported if you use
mysql_query($query) or die(mysql_error());

?

helenp

1:33 pm on Nov 13, 2005 (gmt 0)

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



I donīt get any error doing this:
$query = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES (LAST_INSERT_ID(), '$llegada', '$propiedad', '$half_round')";
mysql_query($query) or die(mysql_error());

This is what is inserted in table bookings:
448 2005-12-01 2005-12-05 Villa_Los_Naranjos test 2005-11-13 test

448 is the id that should be inserted in table propiedades to relate the bookings.

In propiedades this is inserted:
476 2005-12-01 Villa_Los_Naranjos 191 NULL 600.00 0.00 0.00

191 is id_reservas, mysql just picked an old id....that is for an old booking.

RonPK

1:42 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LAST_INSERT_ID() will return the id of the last record inserted. If you use that command in a series of inserts, as you do, it should and will change all the time. So if you always want to use the id of the record inserted into the bookings table, use the $lastid that you've created in posting #1.

helenp

1:50 pm on Nov 13, 2005 (gmt 0)

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



I already tried the $lastid and dindīt work either,
and I have not change the code
trying it again
doing this: $query = "INSERT INTO propiedades (id_reservas, fecha, id_propiedad, importe)" .
"VALUES ('$lastid', '$llegada', '$propiedad', '$half_round')";
mysql_query($query) or die(mysql_error());

table propiedades did insert 0 as id_reservas:
478 2005-12-01 Villa_Los_Naranjos 0 NULL 600.00

This is just incredible, I been using the code for a long time,
and the last insert id has not changed.

helenp

1:59 pm on Nov 13, 2005 (gmt 0)

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



I know now how it changed,
table pagados, (the last one in the insert last inserted id) didnīt have any autoincrement id, phpmyadmin showed it as an error,
so I added on even if I donīt need it.

Therefore the code donīt work anymore,
so If I delete it, it should work again.

But using $lastid should work as well,
but it donīt work?

RonPK

2:00 pm on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to clarify my previous point, in pseudo-code:

1. query(INSERT INTO bookings (..) VALUES (..));

2. $lastID = mysql_insert_id(); // works for any auto-increment column

3. query(INSERT INTO other_table (booking_id, ...) VALUES ($lastID, ..));

4. query(INSERT INTO yet_another_table (booking_id, ...) VALUES ($lastID, ..));

HTH