I have two questions :
I have 3 tables : EMTS , levels, and EMTS_has_levels. It is a many to many relationship between EMTS AND levels
My first question:
In levels table, I have 2 columns, idlevels(primary key) and I have a column called levelname :
CREATE TABLE IF NOT EXISTS `mydb`.`levels` (
`idlevels` INT NOT NULL AUTO_INCREMENT,
`levelname` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idlevels`))
ENGINE = InnoDB;
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:
<input type="checkbox" name="level" value="level1">Level1</input>
<input type="checkbox" name="level" value="level2">Level2</input>
ANd in the register.php I have:
if(isset($_POST['level']))
{
$name=$_POST['level'];
}
$stmt4=$mysqli->prepare("INSERT INTO levels(levelsname) VALUES(?)");
$stmt4->bind_param("s",$name);
$stmt4->execute();
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?
My second question: This is EMTS table:
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;
This is my EMTS_has_levels table:
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 has only two columns that are foriegn keys . First one is levels_idlevels referencing levels table, and the second one is EMTS_idEMTS referencing idEMTS from EMTS table.
My question is say I filled EMTS and I filled levels how can I fill the two foreign keys in EMTS_has_levels?
I tried this but it gives me an error:
$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();
How can I fill the third table that results from the many to many relationship?
levelname, but you're inserting intolevelsname. But why is this form inserting into thelevelstable? If you're linking an EMT to a level, you should only be inserting intobelongs.$stmt4->bind_param("s",$name);as you have a question mark as a placeholder.password VARCHAR(45)- Right away, this suggests an unsafe password hashing method. One such aspassword_hash(), stores a 60 length char. If you plan on using this or are using it already, it already failed you, silently.