Good, I have the Inventory and Model table, in Inventory I have a dropdown that shows me all the models, what I want to do is that when I choose a model from the dropdown and enter the other inventory data, insert the id_model by which the tables that corresponds to the model chosen in the dropdown, I don't know if I am explaining myself correctly, I have tried several insert queries with select but none have been useful.
I leave the tables
I also leave part of the code that I was trying to do.
$nombre_producto = $mysqli->real_escape_string($_REQUEST['nombre_producto']);
$cantidad = $mysqli->real_escape_string($_REQUEST['cantidad']);
$precio = $mysqli->real_escape_string($_REQUEST['precio']);
$modelo = $mysqli->real_escape_string($_REQUEST['modelo']);
$stmt = $mysqli->prepare("INSERT INTO inventario (nombre_producto, cantidad, precio)
VALUES (?, ?, ?)");
$stmt->bind_param("sii", $nombre_producto, $cantidad, $precio);
$stmt->execute();
}
/*
"INSERT INTO inventario (id_modelo, nombre_producto, cantidad, precio)
SELECT mod2.id_modelo, inv.nombre_producto, inv.cantidad, inv.precio
FROM inventario AS inv
JOIN modelos AS mod2
ON inv.id_modelo = mod2.id_modelo
WHERE inv.id_modelo = '?'
AND
"
*/
Thank you !!
Greetings.
Look at mysqli_insert_id ; with that instruction you get the most recent element (that is autoincrement) that was created; Of course, you must consider that it must be the immediate instruction followed by the insert without having closed the connection with which it was made.
That way you get the value of the generated ID to use in your next query that requires it.
UPDATE
I understood from the first moment that you wanted to obtain a newly entered ID; look at your code where
INSERT
you are passing only 3 parameters to it; you are not including$modelo
; sure (I think rather) that this corresponds to theSELECT
one you mention and is not part of the prepare or bind_param of theINSERT
.