MYSQL PHP:在数据库中插入多对多的关系

时间:2017-11-20 16:42:09

标签: php mysql database mysqli

我有两个问题:

我有3个表:EMTS,级别和EMTS_has_levels。这是EMTS与水平之间的多对多关系

我的第一个问题:

levels表中,我有2列,idlevels(主键),我有一个名为levelname的列:

CREATE TABLE IF NOT EXISTS `mydb`.`levels` (
  `idlevels` INT NOT NULL AUTO_INCREMENT,
  `levelname` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idlevels`))
ENGINE = InnoDB;

我想在levelname中插入多个值。 EMT可能有多个级别。所以我所做的是将其添加到html表单:

<input type="checkbox" name="level" value="level1">Level1</input>

<input type="checkbox" name="level" value="level2">Level2</input>

我在register.php中有一个:

if(isset($_POST['level']))
{
    $name=$_POST['level'];
}
$stmt4=$mysqli->prepare("INSERT INTO levels(levelsname) VALUES(?)");
$stmt4->bind_param("s",$name);
$stmt4->execute();

当我尝试上面的PHP代码时,我收到一个错误。 Idlevels不是因为它应该自动增量,但为什么插入到levelsname不起作用?

我的第二个问题: 这是EMTS表:

CREATE TABLE IF NOT EXISTS `mydb`.`EMTS` (
  `idEMTS` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `nickname` VARCHAR(45) NOT NULL,
  `age` VARCHAR(45) NOT NULL,
  `nuber` VARCHAR(45) NOT NULL,
  `email` VARCHAR(45) NULL,
  `city` VARCHAR(255) NOT NULL,
  `password` VARCHAR(45) NOT NULL,
  `shifts_idshifts` INT NULL,
  `bloodtype_id` INT NULL,
  `street` VARCHAR(45) NOT NULL,
  `building` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idEMTS`),
  INDEX `fk_EMTS_shifts1_idx` (`shifts_idshifts` ASC),
  INDEX `fk_EMTS_bloodtype1_idx` (`bloodtype_id` ASC),
  UNIQUE INDEX `nickname_UNIQUE` (`nickname` ASC),
  CONSTRAINT `fk_EMTS_shifts1`
    FOREIGN KEY (`shifts_idshifts`)
    REFERENCES `mydb`.`shifts` (`idshifts`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_EMTS_bloodtype1`
    FOREIGN KEY (`bloodtype_id`)
    REFERENCES `mydb`.`bloodtype` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

这是我的EMTS_has_levels表:

CREATE TABLE IF NOT EXISTS `mydb`.`EMTS_has_levels` (
  `levels_idlevels` INT NULL,
  `EMTS_idEMTS` INT NULL,
  PRIMARY KEY (`levels_idlevels`, `EMTS_idEMTS`),
  INDEX `fk_levels_has_EMTS_EMTS1_idx` (`EMTS_idEMTS` ASC),
  INDEX `fk_levels_has_EMTS_levels1_idx` (`levels_idlevels` ASC),
  CONSTRAINT `fk_levels_has_EMTS_levels1`
    FOREIGN KEY (`levels_idlevels`)
    REFERENCES `mydb`.`levels` (`idlevels`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_levels_has_EMTS_EMTS1`
    FOREIGN KEY (`EMTS_idEMTS`)
    REFERENCES `mydb`.`EMTS` (`idEMTS`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

EMTS_has_levels只有两列是foriegn键。第一个是levels_idlevels引用级别表,第二个是EMTS_idEMTS引用idEMTS表中的EMTS

我的问题是说我已填写EMTS并填写levels如何填写EMTS_has_levels中的两个外键?

我尝试了这个,但它给了我一个错误:

 $stmt10=$mysqli->prepare("INSERT INTO belongs(levels_idlevels) SELECT idlevels FROM levels WHERE levelname=$_POST['levelname'] " );
    $stmt10->execute();
$stmt11=$mysqli->prepare("INSERT INTO belongs(EMTS_idEMTS) SELECT idEMTS FROM EMTS WHERE nickname=$_POST['nickname'] " );
$stmt11->execute();

如何填写由多对多关系产生的第三个表?

1 个答案:

答案 0 :(得分:1)

如果要允许多个级别,则需要使用数组样式名称,以便获得所有级别。

<input type="checkbox" name="level[]" value="level1">Level1</input>
<input type="checkbox" name="level[]" value="level2">Level2</input>

然后你可以遍历所有的值。您需要只插入一次EMTS_has_levels,从level_idlevel表格中获取levels,从EMTS_idEMTS表格中获取EMTS

$stmt4=$mysqli->prepare("INSERT INTO EMTS_has_levels (levels_idlevels, EMTS_idEMTS)
    SELECT l.idlevel, e.idEMTS
    FROM levels AS l
    CROSS JOIN EMTS AS e
    WHERE l.levelname = ?
    AND e.nickname = ?");
$stmt4->bind_param("s",$name, $_POST['nickname']);
foreach ($_POST['level'] as $name) {
    $stmt4->execute();
}