I have a table called scoutboy
and a table called levels
. I have a table called scoutboy_has_levels
that contains two foreign keys : scoutboy_id
and levels_id
.
Table levels
contains a list of all 10 possible categories with each has an id.
Example: A scoutboy (with id 500) who is both nightwatcher(has level id 2) and equipment_chief(has level id 3), upon registration the scoutboy should choose both these two from the form.The input is in an array "levelslist". scoutboy_has_levels should be :
Scoutboy_id levels_idlevels
500 2
500 3
I'm trying to achieve this but it isn't working. My code:
if(isset($_POST['level']))
{
$levelslist=$_POST['level'];}
//$levelslist[0] is in this case "nightwatcher" and at index 1 is is "equipment_chief"
foreach($levelslist as $element)
{
//here I'm retreiving the id for the selected levels from `levels` table
$idlevel=mysqli_query($mysqli,"SELECT idlevels FROM levels WHERE category='$element'");
if(!empty($element)) {
//mysqli_real_escape_string($mysqli,$element);
mysqli_query($mysqli,"INSERT INTO scoutboy_has_levels(levels_idlevels,scoutboy_id) VALUES (mysqli_real_escape_string($mysqli,$element),$id)");
}
}
How to fix this and produce my desired output?