<?php
## Título ##
if ($_POST) {
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$identidad = $_POST['identidad'];
$edad = $_POST['edad'];
$sexo = $_POST['sexo'];
$hijos = $_POST['hijos'];
$telefono = $_POST['telefono'];
$municipios = $_POST['municipios'];//*este es un select de otra tabla municipios que me muestra todos los municipios *//
$ lastvalue = $ municipalities; // * here I get the value of the select to be able to insert it so that it has the same id as person * //
$conexion = mysqli_connect("127.0.0.1", "root", "", "persona_municipio");
$ultimovalor=mysqli_insert_id($conexion);
if ($conexion->connect_error) {
echo('Se produjo un error al intentar realizar la conexión');
} else {
$query = "INSERT INTO persona(nombre,apellido,identidad,edad,sexo,hijos,telefono,id_municipio)
VALUES ('$nombre','$apellido','$identidad','$edad','$sexo','$hijos','$telefono','$ultimovalor')";
if ($conexion->query($query) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . $conexion->error;
}
$conexion->close();
};
};
?>
Error:
INSERT INTO person (name, surname, identity, age, sex, children, telephone, id_municipality) VALUES ('juan', 'veliz', '1517199600702', '22', 'm', '3', '96453034', '0') Can not add or update to child row: a foreign key constraint fails (person_municipality.person, CONSTRAINT persona_ibfk_1 FOREIGN KEY (municipality_id) REFERENCES municipality (municipality_id) ON DELETE CASCADE ON UPDATE CASCADE)