I am inserting data from a form, to two related tables.
At the moment of sending the data, it only saves in one table and in the other it does not save anything. send me the following error
Error: Can not add or update to child row: a foreign key constraint fails (
inversiones
.proyectos
, CONSTRAINTproyectos_ibfk_1
FOREIGN KEY (id_contacto
) REFERENCEScontacto
(id_contacto
) ON UPDATE CASCADE)
This is my php code:
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'inversiones';
$conexion = mysqli_connect($host, $user, $pass) or die ('problemas con el servidor');
mysqli_select_db($conexion, $db) or die ('problemas con la base de datos');
//recuperamos los valores del formulario
$nombre = $_POST['nombre'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$direccion = $_POST['direccion'];
$web = $_POST['web'];
$proyecto = $_POST['proyecto'];
//$empresa = $_POST['empresa'];
$ubicacion = $_POST['ubicacion'];
$municipio = $_POST['municipio'];
$origen = $_POST['origen'];
$sector = $_POST['sector'];
$inversion = $_POST['inversion'];
$empleos = $_POST['empleos'];
$dependencia = $_POST['dependencia'];
$giro = $_POST['giro'];
$fase = $_POST['fase'];
$estatus = $_POST['estatus'];
$semaforo = $_POST['semaforo'];
$query1 = "INSERT INTO contacto
(nombre, telefono, email, direccion, web) VALUES
('$nombre','$telefono','$email','$direccion','$web')";
$resultado = mysqli_query($conexion, $query1);
// Si dio error
if ($resultado === false) {
printf("Error: %s\n", mysqli_error($conexion));
die();
}
$var=mysql_insert_id();
//
$query2 = "INSERT INTO proyectos
(proyecto,ubicacion, municipio, origen, sector, inversion, empleos, dependencia, giro, fase, id_contacto) VALUES
('$proyecto', '$ubicacion','$municipio','$origen','$sector','$inversion','$empleos','$dependencia','$giro','$fase', '$var')";
$resultado = mysqli_query($conexion, $query2);
// Si dio error
if ($resultado === false) {
printf("Error: %s\n", mysqli_error($conexion));
die();
}
header("Location: form_validation.php");
?>
and my database is as follows
That's how it is in the relationship