Hello! I’ve got three tables that I want to link together. When I try to upload images and text throught a form, the web browser show this error message:
Error, query failed 1452-Cannot add or update a child row: a foreign key constraint fails (`mn000532_almacen`.`images`, CONSTRAINT `images_ibfk_1` FOREIGN KEY (`itemID`) REFERENCES `items` (`itemID`) ON DELETE CASCADE)
The itemID field is used in my php script($_GET["itemID"]) to retrieve the all the info about a certain item therefore it needs to be the same in all tables.
CREATE TABLE categories (
categoryID INT(5) NOT NULL,
cat_Description VARCHAR(50),
PRIMARY KEY (itemID)
)TYPE = INNODB;
CREATE TABLE items (
itemID INT(5) NOT NULL AUTO_INCREMENT,
categoryID INT(5) NOT NULL,
itemName CHAR(25) NOT NULL,
item_Description VARCHAR(255),
price CHAR(10),
contactName VARCHAR(50),
phone CHAR(15),
email VARCHAR(50),
website CHAR (25),
submitDate DATE NOT NULL,
expireDate DATE NOT NULL,
PRIMARY KEY(itemID,categoryID),
INDEX (submitDate),
FOREIGN KEY (categoryID) REFERENCES categories (categoryID)
ON DELETE CASCADE
)TYPE = INNODB;
CREATE TABLE images (
imagesID INT (5) NOT NULL AUTO_INCREMENT,
itemID INT (5) NOT NULL,
name VARCHAR (30) NOT NULL,
size INT (11) NOT NULL,
type VARCHAR (30) NOT NULL,
pix MEDIUMBLOB NOT NULL,
PRIMARY KEY(imagesID),
FOREIGN KEY (itemID) REFERENCES items (itemID)
ON DELETE CASCADE
)TYPE = INNODB;
Thank you
Hernan