I'm trying to make an insert with PDO and I get that error
How can I solve it?
This is the code of my tables:
CREATE TABLE 'productos' (
'Id_Producto' int(11) NOT NULL AUTO_INCREMENT,
'Nombre_Producto' varchar(45) DEFAULT NULL,
'Precio_Producto' double DEFAULT NULL,
'Descripcion_producto' varchar(45) DEFAULT NULL,
'Id_Tipo_Producto' int(11) DEFAULT NULL,
'Id_Factura' varchar(45) DEFAULT NULL,
'id_detalle_proveedores' int(11) DEFAULT NULL,
'existencia_producto' int(11) DEFAULT NULL,
'imagen_producto' longblob,
PRIMARY KEY ('Id_Producto'),
KEY 'fk_Productos_Factura_idx' ('Id_Factura'),
KEY 'fk_Productos_Detalle_Proveedores_idx' ('id_detalle_proveedores'),
KEY 'fk_Productos_Tipo_Producto_idx' ('Id_Tipo_Producto'),
CONSTRAINT 'fk_Productos_Detalle_Proveedores' FOREIGN KEY ('id_detalle_proveedores') REFERENCES 'detalle_proveedores' ('id_detalle_proveedores') ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT 'fk_Productos_Factura' FOREIGN KEY ('Id_Factura') REFERENCES 'factura' ('Id_Folio_Factura') ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT 'fk_Productos_Tipo_Producto' FOREIGN KEY ('Id_Tipo_Producto') REFERENCES 'tipo_producto' ('Id_Tipo_Producto') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=74 DEFAULT CHARSET=utf8
And this is the other table:
CREATE TABLE 'tipo_producto' (
'Id_Tipo_Producto' int(11) NOT NULL,
'Nombre_tipo_Producto' varchar(45) DEFAULT NULL,
PRIMARY KEY ('Id_Tipo_Producto')
) ENGINE=InnoDB DEFAULT CHARSET=utf8
This is the code with which I insert:
public function insertarData($datos=array())
{
$sql=$this->conexion->prepare("insert into productos (nombre_producto,precio_producto,descripcion_producto,Id_Tipo_Producto,id_factura,id_detalle_proveedores,existencia_producto,imagen_producto) values (:nombre,:precio,:descripcion,:tipo,:factura,:lote,:existencia,:foto)");
$sql->bindParam(':nombre',$nombre);
$sql->bindParam(':precio',$precio);
$sql->bindParam(':descripcion',$descripcion);
$sql->bindParam(':tipo',$tipo);
$sql->bindParam(':factura',$fact);
$sql->bindParam(':lote',$lote);
$sql->bindParam(':existencia',$existencia);
$sql->bindParam(':foto',$foto);
$nombre=$datos[0];
$precio=$datos[1];
$descripcion=$datos[2];
$tipo=$datos[3];
$lote=$datos[4];
$existencia=$datos[5];
$foto=$datos[6];
$fact=NULL;
$sql->execute();
// $this->conexion->exec($sql);
}
<?php
require "Parametros.php";
class Conexion
{
public $conexion=null;
public function __construct(){
$parametros=sprintf('mysql:host=%s;dbname=%s',Parametros::HOST,Parametros::DBNAME);
try
{
$this->conexion = new PDO($parametros, Parametros::USUARIO, Parametros::PASSWORD,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (PDOException $e)
{
echo "Failed to get DB handle: " . $e->getMessage() . "<br/>";
die();
}
}
}
Fatal error: Uncaught PDOException: SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails (
punto_de_venta
.productos
, CONSTRAINTfk_Productos_Tipo_Producto
FOREIGN KEY (Id_Tipo_Producto
) REFERENCEStipo_producto
(Id_Tipo_Producto
) ON DELETE CASCADE ON UPDATE CASCADE) in C: \ xamppp \ htdocs \ PRACTICES \ Web_Petco \ model \ Products.php: 44 Stack trace: # 0 C: \ xamppp \ htdocs \ PRACTICE \ Web_Petco \ model \ Products.php (44): PDOStatement-> execute () # 1 C: \ xamppp \ htdocs \ PRACTICE \ Web_Petco \ controller \ ControllerFormProductos.php (16): Products-> insertData (Array) # 2 {main} thrown in C: \ xamppp \ htdocs \ PRACTICES \ Web_Petco \ model \ Products.php on line 44