Forum Moderators: coopster
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
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?
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.
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.
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?
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