Problems when inserting data into a table with several foreign keys

0

Good, I am trying to insert data in all the tables of my database, especially the one of student that depends on three foreign keys already obtained when executing the previous querys. I will leave all the queries, but I would like them to detail the one of students that is the one that produces the error, thank you very much.

Here is the error that I get when executing the query:

            //Registro de representantes        
$registroRepresentante = "insert into representantes (tipoCedulaRepresentante, cedulaRepresentante, primerApellidoRepresentante, segundoApellidoRepresentante, primerNombreRepresentante, segundoNombreRepresentante, edadRepresentante, direccionRepresentante, codigoTelefonoRepresentante, telefonoRepresentante, correoRepresentante, direccionTrabajoRepresentante, codigoTelefonoTrabajoRepresentante, telefonoTrabajoRepresentante, direccionEmergencia, fk_parentesco, fk_profesion) values (
    '$_POST[tipoCedulaRepresentante]','$_POST[cedulaRepresentante]','$_POST[primerApellidoRepresentante]',
    '$_POST[segundoApellidoRepresentante]','$_POST[primerNombreRepresentante]','$_POST[segundoNombreRepresentante]',
    $_POST[edadRepresentante],'$_POST[direccionRepresentante]','$_POST[codigoTelefonoRepresentante]',
    '$_POST[telefonoRepresentante]','$_POST[correoRepresentante]','$_POST[direccionTrabajoRepresentante]',
    '$_POST[codigoTelefonoTrabajoRepresentante]','$_POST[telefonoTrabajoRepresentante]','$_POST[direccionEmergencia]',
    '$_POST[parentescoRepresentante]','$_POST[profesionRepresentante]')";

    $registrarRepresentante = mysqli_query($conexion,$registroRepresentante) or die ("Problemas en el query".mysqli_error($conexion));
    echo "ingresaron datos representantes";

    //Consulta del ultimo representante ingresado para optener su clave foranea
$consultarRepresentante = mysqli_query($conexion, "select max(id_representante) from representantes")  or die ("Problemas en el query".mysqli_error($conexion));
    $registros = mysqli_fetch_array($consultarRepresentante);
    echo $registros[0]."<br>";
    $foraneaRepresentante = '$registro[0]';


    //Registro de los padres
$registroPadres =  "insert into padres (tipoCedulaPadre, cedulaPadre, primerApellidoPadre, segundoApellidoPadre, primerNombrePadre, segundoNombrePadre, direccionPadre, codigoTelefonoPadre, telefonoPadre) values ('$_POST[tipoCedulaPadre]','$_POST[cedulaPadres]',
    '$_POST[primerApellidoPadres]','$_POST[segundoApellidoPadres]','$_POST[primerNombrePadres]','$_POST[segundoNombrePadres]',
    '$_POST[direccionPadres]','$_POST[codigoTelefonoPadres]','$_POST[telefonoPadres]') ";

    $registrarPadres = mysqli_query($conexion,$registroPadres) or die ("Problemas en el query".mysqli_error($conexion));
    echo "ingresaron datos padres";

    //Consulta del ultimo padre ingresado para optener su clave foranea
$consultarPadre = mysqli_query($conexion, "select max(id_padre) from padres")  or die ("Problemas en el query".mysqli_error($conexion));
    $registros = mysqli_fetch_array($consultarPadre);
    echo $registros[0]."<br>";
    $foraneaPadre = '$registro[0]';


    //Registro del estatus de los alumnos
$registroEstatusAlumno = "insert into estatusalumno (estadoAlumno, institutoProcedencia, asignaturaQueCursa, fechaInscripcion) values (
    '$_POST[estadoAlumno]','$_POST[institutoProcedencia]','$_POST[asignaturaCursa]','$fechaActual')";

    $registrarEstatusAlumno = mysqli_query($conexion,$registroEstatusAlumno) or die ("Problemas en el query".mysqli_error($conexion));
    echo "ingresaron datos de estatus";

    //Consulta del ultimo estatus ingresado para optener su clave foranea
$consultarEstatus = mysqli_query($conexion,"select max(id_estatus) from estatusalumno") or die ("Problemas en el query".mysqli_error($conexion));
    $registros = mysqli_fetch_array($consultarEstatus);
    echo $registros[0]."<br>";
    $foraneaEstatus = '$registro[0]';


    //Registro del estatus de los alumnos
$registroAlumno = "insert into alumnos (tipoCedulaAlumno, cedulaAlumno, primerApellidoAlumno, segundoApellidoAlumno, primerNombreAlumno, segundoNombreAlumno, fechaNacimientoAlumno, edadAlumno, direccionAlumno, codigoTelefonoAlumno, telefonoAlumno, correoAlumno, fk_representante, fk_padre, fk_estatusalumno) values ('$_POST[tipoCedulaAlumno]','$_POST[cedulaAlumno]','$_POST[primerApellidoAlumno]','$_POST[segundoApellidoAlumno]','$_POST[primerNombreAlumno]','$_POST[segundoNombreAlumno]','$_POST[fechaNacimientoAlumno]',$edadac,'$_POST[direccionAlumno]','$_POST[codigoTelefonoAlumno]','$_POST[telefonoAlumno]','$_POST[correoAlumno]', 
    '$foraneaRepresentante','$foraneaPadre','$foraneaEstatus')";

    $registrarAlumnos = mysqli_query($conexion,$registroAlumno) or die ("Problemas en el query: ".mysqli_error($conexion));
    echo "ingresaron datos alumnos";

    //Consulta del ultimo alumno ingresado para optener su clave foranea
$consultarAlumno = mysqli_query($conexion,"select max(id_alumno) from alumnos") or die ("Problemas en el query".mysqli_error($conexion));
    $registros = mysqli_fetch_array($consultarAlumno);
    echo $registro[0]."<br>";
    $foraneaAlumno = '$registro[0]';
    
asked by shadowmors 20.11.2016 в 06:04
source

1 answer

1

You have to be careful when you create a FK between tables that already contain data, it is very likely that at least one record does not comply with the restriction, and therefore MySQL will trigger the error.

Another point to keep in mind, will reject any operation INSERT or UPDATE that tries to create a foreign key value in a secondary table if there is no matching candidate key value in the main table.

First I would check, what I mentioned, to find the fault, otherwise you could upload some diagrama E-R of base de datos .

Then I would also modify the $_POST[$tu_variable] , I would add '' between the [] , it would be like this $_POST['$tu_variable']

    
answered by 20.11.2016 в 12:39