0
create table cliente (
id_cliente integer auto_increment,
nome varchar (100),
primary key (id_cliente)
);
create table produto (
id_produto integer auto_increment,
id_cliente integer,
nome_produto varchar (100),
referencia varchar (100),
valor double (2,2),
index id (id_cliente),
primary key (id_produto),
CONSTRAINT produto_ibfk_1 FOREIGN KEY (id_cliente) REFERENCES cliente (id_cliente) ON UPDATE CASCADE
);
I have these two tables that are both relating. When I try to insert data in the second table happens following php error:
Cannot add or update a Child Row: a Foreign key Constraint fails (
sistema
.produto
, CONSTRAINTproduto_ibfk_1
FOREIGN KEY (id_cliente
) REFERENCEScliente
(id_cliente
) ON UPDATE CASCADE)'
Note: I have data entered in the client table.
$query = "INSERT INTO " . $this -> name_entity . "("; $sep = ""; foreach ($fields as $field) { $query .= $sep . $field; $sep = ", "; } $query .= " ) VALUES ( "; $sep = ""; foreach ($fields as $field) { $query .= $sep . ":" . $field; $sep = ", "; } $query .= " ); ";
$connection = ConnectionDB::openConnection();
$result = $connection -> prepare($query);
foreach ($fields as $field) {
$value = $atributos[$field];
$result -> bindValue(":" . $field, $value);
}
$result -> execute();
$this -> reportarErros($result);
$ultimoId = $connection -> lastInsertId();
ConnectionDB::closeConnection();
return $ultimoId;