Insert data in related tables-. error when inserting

0

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 , CONSTRAINT proyectos_ibfk_1   FOREIGN KEY ( id_contacto ) REFERENCES contacto ( 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

    
asked by Brandon Ramsel Vite 22.11.2016 в 10:38
source

2 answers

0

The detail is that you can not first add / update a record in a table that contains a relation and a field with a foreign key.

Ideally, you must first add a record to the table containing the primary key and relate it to another table by means of a field (containing the foreign key) and be identical in both the data type and the length of the data.

If you have any further questions, you can read this MySQL link, here Which says something very important there:

  

Foreign key relationships involve a parent table that holds the   central data values, and a child table with identical values pointing   back to its parent. The FOREIGN KEY clause is specified in the child   table The parent and child tables must use the same storage engine.   They must not be TEMPORARY tables.

    
answered by 22.11.2016 / 16:55
source
0

Consider that:

  

mysqli_query : Return FALSE in error case. If a query of type SELECT, SHOW, DESCRIBE or EXPLAIN is successful, mysqli_query () will return an object mysqli_result . For other successful queries of mysqli_query () you will return TRUE .

//

With this info, to know if it gave an error and why, you could modify your script in the following way:

<?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 empresas 
           (proyecto, empresa, ubicacion, municipio, origen, sector, inversion, empleos, dependencia, giro, fase, id_contacto) VALUES 
           ('$proyecto','$empresa','$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");  
?>
    
answered by 22.11.2016 в 11:11