I have two related tables and I want to get the last one ID_primeraTabla
to insert it into the second table since it ID_primeraTabla
is the foreign key. any ideas ?
What I have is the following
$pregunta = $_POST["pregunta"];
$respuesta = $_POST["respuesta"];
$comentario = $_POST["comentario"];
$sql = "Select id_asistente FROM Asistente order by id_asistente DESC LIMIT 1";
mysqli_query($connect, $sql) or die (mysqli_error($connect));
$query = "Insert into Encuesta_asistente(pregunta_encuesta,respuesta_encuesta,comentario_encuesta,id_asistente) values ('$pregunta','$respuesta','$comentario','$sql');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
The problem is that you're not getting the value of the first query anywhere.
Here is the complete code with comments:
Important note : Please use prepared statements
MySQLi
or betterPDO
to avoid possible SQL Injection attacks.