我有3个名为book
,author
,publisher
的表格。书和作者之间的关系是一个名为book_author_link
的解析表。主要问题是,当用户添加书籍时,由于某些字段属于多个表格,因此未成功添加。因此,我目前遇到INSERT
查询问题。我该怎么做呢?
这是我的PHP:
$queryInsert = "INSERT INTO book,author,
publishers(title,isbn,author_name,publisher_name,year_published, book_desc,genre_id,keywords)
FROM book b, author a ,book_author_link ba, publishers p
VALUES ('$title',$isbn,'$author_name','$publisher_name',$year_published,'$genre_type','$book_desc','$genre_id','$keywords')
WHERE b.book_id = ba.book_id AND a.author_id = ba.author_id AND b.publisher_id = p.publisher_id";
这是我的书桌:
CREATE TABLE IF NOT EXISTS `fyp`.`book` (
`book_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`ISBN` VARCHAR(45) NOT NULL,
`book_desc` VARCHAR(100) NOT NULL,
`year_published` VARCHAR(45) NOT NULL,
`year_of_birth` YEAR NOT NULL,
`image` VARCHAR(45) NULL,
`genre_id` INT NOT NULL,
`publisher_id` INT NOT NULL,
`user_id` INT NOT NULL,
PRIMARY KEY (`book_id`),
INDEX `fk_book_publishers1_idx` (`publisher_id` ASC),
INDEX `fk_book_user1_idx` (`user_id` ASC),
INDEX `fk_book_genre1_idx` (`genre_id` ASC),
CONSTRAINT `book_publishers_key`
FOREIGN KEY (`publisher_id`)
REFERENCES `fyp`.`publishers` (`publisher_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `book_user_key`
FOREIGN KEY (`user_id`)
REFERENCES `fyp`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `book_genre_key`
FOREIGN KEY (`genre_id`)
REFERENCES `fyp`.`genre` (`genre_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
- 表fyp
。author
CREATE TABLE IF NOT EXISTS `fyp`.`author` (
`author_id` INT NOT NULL AUTO_INCREMENT,
`author_name` VARCHAR(45) NOT NULL,
`year_of_birth` YEAR NOT NULL,
`year_deceased` YEAR NULL,
`image` VARCHAR(45) NULL,
`user_id` INT NOT NULL,
`country_id` INT NOT NULL,
`gender_id` INT NOT NULL,
PRIMARY KEY (`author_id`),
INDEX `fk_author_user1_idx` (`user_id` ASC),
INDEX `fk_author_country1_idx` (`country_id` ASC),
INDEX `fk_author_gender1_idx` (`gender_id` ASC),
CONSTRAINT `author_user_key`
FOREIGN KEY (`user_id`)
REFERENCES `fyp`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `author_country_key`
FOREIGN KEY (`country_id`)
REFERENCES `fyp`.`country` (`country_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `author_gender_key`
FOREIGN KEY (`gender_id`)
REFERENCES `fyp`.`gender` (`gender_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
- 表fyp
。publishers
DROP TABLE IF EXISTS `fyp`.`publishers` ;
CREATE TABLE IF NOT EXISTS `fyp`.`publishers` (
`publisher_id` INT NOT NULL,
`publisher_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`publisher_id`))
ENGINE = InnoDB;
- 表fyp
。book_author_link
DROP TABLE IF EXISTS fyp
。book_author_link
;
CREATE TABLE IF NOT EXISTS `fyp`.`book_author_link` (
`book_author_link_id` INT NOT NULL AUTO_INCREMENT,
`book_id` INT NOT NULL,
`author_id` INT NOT NULL,
PRIMARY KEY (`book_author_link_id`),
INDEX `fk_book_author_link_book_idx` (`book_id` ASC),
INDEX `fk_book_author_link_author1_idx` (`author_id` ASC),
CONSTRAINT `book_key`
FOREIGN KEY (`book_id`)
REFERENCES `fyp`.`book` (`book_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `author_key`
FOREIGN KEY (`author_id`)
REFERENCES `fyp`.`author` (`author_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
答案 0 :(得分:2)
我建议在交易中运行查询:http://php.net/manual/pdo.transactions.php
此外,您可能需要此功能来获取AUTO_INCREMENT ID:http://php.net/manual/de/pdo.lastinsertid.php
您使用phpMyAdmin作为GUI吗?你试过在那里运行SQL吗?如果存在"关系问题",则应该打印确切的错误消息。你应该在这里提供。
答案 1 :(得分:0)
您可以尝试在主表上插入后触发器。