Good I have a problem currently in my code, I'm working on a student registration system, where a representative can enroll several students, but if I delete a student I do not want the representative's data to be deleted because this may have another registered student, it would only be deleted if and only if it no longer has registered students.
PD: it is worth mentioning that the CedulaRepresentanteRef is in the table students and is the foreign key of CedulaRepresentante (Primary Key) located in the representative table.
<?php
$Conexion = mysqli_connect("localhost","root","","prueba2") or die ("No se pudo realizar la conexion");
$Registros = mysqli_query($Conexion, "select * from documentos, estatusalumno, padres, alumnos inner join representantes on alumnos.CedulaRepresentanteRef = representantes.CedulaRepresentante where alumnos.CedulaAlumno = '$_REQUEST[CedulaAlumno]'") or die ("Problemas en el select: ".mysqli_error($Conexion));
if ($Reg = mysqli_fetch_array($Registros))
$CedulaRepresentanteRef = $Reg['CedulaRepresentanteRef'];
{
mysqli_query($Conexion, "delete from alumnos where CedulaAlumno = '$_REQUEST[CedulaAlumno]'")
or die ("Problemas en el select: ".mysqli_error($Conexion));
mysqli_query($Conexion, "delete from estatusalumno where CedulaAlumno = '$_REQUEST[CedulaAlumno]'")
or die ("Problemas en el select: ".mysqli_error($Conexion));
mysqli_query($Conexion, "delete from padres where CedulaAlumno = '$_REQUEST[CedulaAlumno]'")
or die ("Problemas en el select: ".mysqli_error($Conexion));
mysqli_query($Conexion, "delete from documentos where CedulaAlumno = '$_REQUEST[CedulaAlumno]'")
or die ("Problemas en el select: ".mysqli_error($Conexion));
$Registros2 = mysqli_query($Conexion, "select * from alumnos, representantes") or die
("Problemas en el segundo registro: ".mysqli_error($Conexion));
$Reg2 = mysqli_fetch_array($Registros2);
if ($CedulaRepresentanteRef <> $Reg2['CedulaRepresentante'])
{
mysqli_query($Conexion, "delete from representantes where CedulaRepresentante = $CedulaRepresentanteRef")
or die ("Problemas en el select: ".mysqli_error($Conexion));
}
else
{
echo "Se ha eliminado el alumno";
}
}
else
{
echo "No se encontro el alumno";
}
mysqli_close($Conexion);
?>