title: MYSQL PHP: Inserting into database many to many relationships
body: <p>I have two questions :</p>
  
  <p>I have 3 tables : EMTS , levels, and EMTS_has_levels. It is a many to many relationship between EMTS AND levels</p>
  
  <p>My first question:</p>
  
  <p>In <code>levels</code> table, I have 2 columns, idlevels(primary key)  and I have a column called <code>levelname</code> :</p>
  
  <hr>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `mydb`.`levels` (
    `idlevels` INT NOT NULL AUTO_INCREMENT,
    `levelname` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`idlevels`))
  ENGINE = InnoDB;
  </code></pre>
  
  <p>I want to insert multiple values to levelname. AN EMT might have multiple levels. SO what I did is add this to the  html form:</p>
  
  <p><code>&lt;input type="checkbox" name="level" value="level1"&gt;Level1&lt;/input&gt;</code></p>
  
  <p><code>&lt;input type="checkbox" name="level" value="level2"&gt;Level2&lt;/input&gt;</code></p>
  
  <p>ANd in the register.php I have:</p>
  
  <pre><code>if(isset($_POST['level']))
  {
      $name=$_POST['level'];
  }
  $stmt4=$mysqli-&gt;prepare("INSERT INTO levels(levelsname) VALUES(?)");
  $stmt4-&gt;bind_param("s",$name);
  $stmt4-&gt;execute();
  </code></pre>
  
  <p>When I try the above php code I get an error. Idlevels isn't the reason as it should autoincrements, but why isn't the insertion to levelsname working?</p>
  
  <p>My second question:
  This is EMTS table:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>This is my EMTS_has_levels table:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p><code>EMTS_has_levels</code> has only two columns that are foriegn keys . First one is <code>levels_idlevels</code> referencing levels table, and the second one is <code>EMTS_idEMTS</code> referencing <code>idEMTS</code> from <code>EMTS</code> table.</p>
  
  <p>My question is say I filled <code>EMTS</code> and I filled <code>levels</code> how can I fill the two foreign keys in <code>EMTS_has_levels</code>?</p>
  
  <p>I tried this but it gives me an error:</p>
  
  <pre><code> $stmt10=$mysqli-&gt;prepare("INSERT INTO belongs(levels_idlevels) SELECT idlevels FROM levels WHERE levelname=$_POST['levelname'] " );
      $stmt10-&gt;execute();
  $stmt11=$mysqli-&gt;prepare("INSERT INTO belongs(EMTS_idEMTS) SELECT idEMTS FROM EMTS WHERE nickname=$_POST['nickname'] " );
  $stmt11-&gt;execute();
  </code></pre>
  
  <p>How can I fill the third table that results from the many to many relationship?</p>