Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

Error mysql 1452 al insertar registros tablas relacionadas

Estas en el tema de Error mysql 1452 al insertar registros tablas relacionadas en el foro de Mysql en Foros del Web. tengo 3 tablas que son responsable(idresponsable(pk),nombre,apellido,sexo ,fecha_naci,direccion,telefono,correo,(idcharla,id institucion(fk)) charla:(idcharla(pk),tipo_charla,cantidad_particip antes,tipo_participantes,lugar_charla,fecha_dispon ible) institucion:(idinstitucion(pk),institucion,direcci on,telefono,tipo_institucion) LO QUE NECESITO HACER ES INSERTAR DATOS POR MEDIO DEL FORMULARIO HTML,PHP ...
  #1 (permalink)  
Antiguo 20/04/2012, 21:32
Avatar de dangerouscl  
Fecha de Ingreso: abril-2012
Ubicación: Acarigua, Venezuela
Mensajes: 6
Antigüedad: 12 años
Puntos: 0
Error mysql 1452 al insertar registros tablas relacionadas

tengo 3 tablas que son responsable(idresponsable(pk),nombre,apellido,sexo ,fecha_naci,direccion,telefono,correo,(idcharla,id institucion(fk))


charla:(idcharla(pk),tipo_charla,cantidad_particip antes,tipo_participantes,lugar_charla,fecha_dispon ible)


institucion:(idinstitucion(pk),institucion,direcci on,telefono,tipo_institucion)

LO QUE NECESITO HACER ES INSERTAR DATOS POR MEDIO DEL FORMULARIO HTML,PHP Y ME MANDA ESE ERROR.
  #2 (permalink)  
Antiguo 21/04/2012, 05:59
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Error mysql 1452 al insertar registros tablas relacionadas

El error 1452 tiene este mensaje:
Cita:
Mensaje: Cannot add or update a child row: a foreign key constraint fails (%s)
Eso significa que no puede agregar o actualizar un registro porque falla la restricción de Foreign Key.
En otras palabras, la tabla en cuestión tiene declarada al menos una FK, y los valores que intentas ingresar en ese o esos campos no existen en la tabla referida, por lo que no admite esa operación.
Cuando tienes una dependencia entre dos tablas, el registro correspondiente a la tabla a la que la FK apunta debe ser la primera en ser insertado.
Siempre.

En tu caso, significa que primero tienes que cargar Institución, luego Charla y recién entonces Responsable.
¿Se entiende?

PD: Lo que quieras hacer con el formulario en HTML/PHP no es tema de este foro. Es irrelevante en el problema.
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #3 (permalink)  
Antiguo 21/04/2012, 18:48
Avatar de dangerouscl  
Fecha de Ingreso: abril-2012
Ubicación: Acarigua, Venezuela
Mensajes: 6
Antigüedad: 12 años
Puntos: 0
Respuesta: Error mysql 1452 al insertar registros tablas relacionadas

ok si entiendo! lo que quiero es cargar todos esos datos por medio del formulario en ese caso como haria para cargas los datos! trabajo por medio del modelo MVC
modelo: clsCharla.php


<?
require_once("clsDatos.php");
class clsCharla extends clsDatos
{
private $acCedula;
private $acNombre;
private $acApellido;
private $acSexo;
private $acFechaNacimiento;
private $acDireccion;
private $acTelefono;
private $acCorreo;
private $acCodigoCharla;
private $acTipoCharla;
private $acCantidadParticipantes;
private $acTipoParticipantes;
private $acLugarCharla;
private $acFechaDisponible;
private $acNombreInstitucion;
private $acDireccionInstitucion;
private $acTelefonoInstitucion;
private $acTipoInstitucion;

public function __construct()
{
$this->acCedula="";
$this->acNombre="";
$this->acApellido="";
$this->acSexo="";
$this->acFechaNacimiento="";
$this->acDireccion="";
$this->acTelefono="";
$this->acCorreo="";
$this->acCodigoCharla="";
$this->acTipoCharla="";
$this->acCantidadParticipantes="";
$this->acTipoParticipantes="";
$this->acLugarCharla="";
$this->acFechaDisponible="";
$this->acNombreInstitucion="";
$this->acDireccionInstitucion="";
$this->acTelefonoInstitucion="";
$this->acTipoInstitucion="";
}

public function __destruct()
{
}

public function setcCedula($pcCedula)
{
$this->acCedula=$pcCedula;
}

public function setcNombre($pcNombre)
{
$this->acNombre=$pcNombre;
}

public function setcApellido($pcApellido)
{
$this->acApellido=$pcApellido;
}

public function setcSexo($pcSexo)
{
$this->acSexo=$pcSexo;
}

public function setcFechaNacimiento($pcFechaNacimiento)
{
$this->acFechaNacimiento=$pcFechaNacimiento;
}

public function setcDireccion($pcDireccion)
{
$this->acDireccion=$pcDireccion;
}

public function setcTelefono($pcTelefono)
{
$this->acTelefono=$pcTelefono;
}

public function setcCorreo($pcCorreo)
{
$this->acCorreo=$pcCorreo;
}

public function setcCodigoCharla($pcCodigoCharla)
{
$this->acCodigoCharla=$pcCodigoCharla;
}

public function setcTipoCharla($pcTipoCharla)
{
$this->acTipoCharla=$pcTipoCharla;
}

public function setcCantidadParticipantes($pcCantidadParticipantes )
{
$this->acCantidadParticipantes=$pcCantidadParticipante s;
}

public function setcTipoParticipantes($pcTipoParticipantes)
{
$this->acTipoParticipantes=$pcTipoParticipantes;
}

public function setcLugarCharla($pcLugarCharla)
{
$this->acLugarCharla=$pcLugarCharla;
}

public function setcFechaDisponible($pcFechaDisponible)
{
$this->acFechaDisponible=$pcFechaDisponible;
}

public function setcNombreInstitucion($pcNombreInstitucion)
{
$this->acNombreInstitucion=$pcNombreInstitucion;
}

public function setcDireccionInstitucion($pcDireccionInstitucion)
{
$this->acDireccionInstitucion=$pcDireccionInstitucion;
}

public function setcTelefonoInstitucion($pcTelefonoInstitucion)
{
$this->acTelefonoInstitucion=$pcTelefonoInstitucion;
}

public function setcTipoInstitucion($pcTipoInstitucion)
{
$this->acTipoInstitucion=$pcTipoInstitucion;
}

public function getcCedula()
{
return $this->acCedula;
}

public function getcNombre()
{
return $this->acNombre;
}

public function getcApellido()
{
return $this->acApellido;
}

public function getcSexo()
{
return $this->acSexo;
}

public function getcFechaNacimiento()
{
return $this->acFechaNacimiento;
}

public function getcDireccion()
{
return $this->acDireccion;
}

public function getcTelefono()
{
return $this->acTelefono;
}

public function getcCorreo()
{
return $this->acCorreo;
}

public function getcCodigoCharla()
{
return $this->acCodigoCharla;
}

public function getcTipoCharla()
{
return $this->acTipoCharla;
}

public function getcCantidadParticipantes()
{
return $this->acCantidadParticipantes;
}

public function getcTipoParticipantes()
{
return $this->acTipoParticipantes;
}

public function getcLugarCharla()
{
return $this->acLugarCharla;
}

public function getcFechaDisponible()
{
return $this->acFechaDisponible;
}

public function getcNombreInstitucion()
{
return $this->acNombreInstitucion;
}

public function getcDireccionInstitucion()
{
return $this->acDireccionInstitucion;
}

public function getcTelefonoInstitucion()
{
return $this->acTelefonoInstitucion;
}

public function getcTipoInstitucion()
{
return $this->acTipoInstitucion;
}

public function buscar()
{
$llEnc=false;
$lcSql="select * from responsable,charla,institucion where(cedula='$this->acCedula') and charla.idcharla=responsable.idcharla and institucion.idinstitucion=responsable.idinstitucio n";
$this->conectar();
$lrTb=$this->filtro($lcSql);
if($laRow=$this->proximo($lrTb))
{
$this->acCedula=$laRow["cedula"];
$this->acNombre=$laRow["nombre"];
$this->acApellido=$laRow["apellido"];
$this->acSexo=$laRow["sexo"];
$this->acFechaNacimiento=$laRow["fecha_naci"];
$this->acDireccion=$laRow["direccion"];
$this->acTelefono=$laRow["telefono"];
$this->acCorreo=$laRow["correo"];
$this->acCodigoCharla=$laRow["idcharla"];
$this->acTipoCharla=$laRow["tipo_charla"];
$this->acCantidadParticipantes=$laRow["cantidad_participantes"];
$this->acTipoParticipantes=$laRow["tipo_participantes"];
$this->acLugarCharla=$laRow["lugar_charla"];
$this->acFechaDisponible=$laRow["fecha_disponible"];
$this->acNombreInstitucion=$laRow["idinstitucion"];
$this->acDireccionInstitucion=$laRow["direccion"];
$this->acTelefonoInstitucion=$laRow["telefono"];
$this->acTipoInstitucion=$laRow["tipo_institucion"];
$llEnc=true;
}
$this->cierrafiltro($lrTb);
$this->desconectar();
return $llEnc;
}

public function incluir()
{
$llHecho=false;
$this->conectar();
$lcSql="insert into responsable(cedula,nombre,apellido,sexo,fecha_naci ,direccion,telefono,correo) values ('$this->acCedula','$this->acNombre','$this->acApellido','$this->acSexo','$this->acFechaNacimiento','$this->acDireccion','$this->acTelefono','$this->acCorreo')";
$llHecho=$this->ejecutar($lcSql);
$lcSql="insert into charla(idcharla,tipo_charla,cantidad_participantes ,tipo_participantes,lugar_charla,fecha_disponible) values ('$this->acCodigoCharla','$this->acTipoCharla','$this->acCantidadParticipantes','$this->acTipoParticipantes','$this->acLugarCharla','$this->acFechaDisponible')";
$llHecho=$this->ejecutar($lcSql);
$lcSql="insert into institucion(idinstitucion,direccion,telefono,tipo_ institucion) values ('$this->acNombreInstitucion','$this->acDireccionInstitucion','$this->acTelefonoInstitucion','$this->acTipoInstitucion')";
$llHecho=$this->ejecutar($lcSql);
$this->desconectar();
return $llHecho;
}

public function modificar()
{
$llHecho=false;
$this->conectar();
$lcSql="update responsable set nombre='$this->acNombre',apellido='$this->acApellido',sexo='$this->acSexo',fecha_naci='$this->acFechaNacimiento',direccion='$this->acDireccion',telefono='$this->acTelefono',correo='$this->acCorreo' where (cedula='$this->acCedula')";
$llHecho=$this->ejecutar($lcSql);
$lcSql="update charla set tipo_charla='$this->acTipoCharla',cantidad_participantes='$this->acCantidadParticipantes',tipo_participantes='$thi s->acTipoParticipantes',lugar_charla='$this->acLugarCharla',fecha_disponible='$this->acFechaDisponible' where (idcharla='$this->acCodigoCharla')";
$llHecho=$this->ejecutar($lcSql);
$lcSql="update institucion set direccion='$this->acDireccionInstitucion',telefono='$this->acTelefonoInstitucion',tipo_institucion='$this->acTipoInstitucion' where (idinstitucion='$this->acNombreInstitucion')";
$llHecho=$this->ejecutar($lcSql);
$this->desconectar();
return $llHecho;

}

public function eliminar()
{
$llHecho=false;
$lcSql="delete from responsable where (cedula='$this->acCedula')";
$this->conectar();
$llHecho=$this->ejecutar($lcSql);
$this->desconectar();
return $llHecho;
}
}
?>
  #4 (permalink)  
Antiguo 21/04/2012, 18:51
Avatar de dangerouscl  
Fecha de Ingreso: abril-2012
Ubicación: Acarigua, Venezuela
Mensajes: 6
Antigüedad: 12 años
Puntos: 0
Respuesta: Error mysql 1452 al insertar registros tablas relacionadas

lo q pasa es q incluyo todos los datos por medio de un solo formulario dond se guarda en esa 3 tablas! como haria en mi caso....
  #5 (permalink)  
Antiguo 22/04/2012, 09:45
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Error mysql 1452 al insertar registros tablas relacionadas

El que ingreses todos los datos en un mismo formulario, si lo razonas bien, es irrelevante para la base de datos, porque el proceso de inserción no lo haces en el formulario, sino en los scripts de PHP. Entonces es en ellos donde debes programar los procesos de inserción en base a los datos recibidos de modo que respeten la consistencia e integridad de datos de la base.
¿No te parece?
Pero en este punto te recuerdo un detalle: Este foro está dedicado a los problemas de SQL y Base de Datos; no se deben tratar asuntos de programación ni postear código de programación acá (Función de la sección de Base de Datos), porque cada lenguaje tiene su propio foro, donde se tratan los asuntos relacionados con ellos.
El cómo se hace esto en PHP es una pregunta cuya respuesta que encontrarás en el Foro de PHP, que es donde están los expertos en ese lenguaje. Recuerda que el que uses MySQL no implica necesariamente usar PHP, ya que son cosas independientes. De hecho yo programo preferentemente en C#.Net y VB.Net, no en PHP (aunque suelo hacer algunas cosas en él).
Pregunta en el Foro de PHP, y es más, te sugeriría que primero le des una mirada a las FAQ's de PHP. Son muy útiles.
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)

Etiquetas: tablas
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:57.